javascript - How to parse Rest Api json response and insert data to Jade template (Node.js + express)? -


so, need parse json response in node.js + express , insert data jade file. in sinatra, easy, here.. response format like:

{   "status": "200",   "name": "",   "port": "7777",   "playercount": "4",   "players": "name, of, player" } 

express's res.render() method allows pass in local template variables, , use them in template. example:

app.route('/', function (req, res) {   // code response, , example's sake, i'll it's assigned 'view_data'.   if (typeof view_data === 'string') {     // if know sure if data going object or string,      // can leave if statement out, , instead parse (or not if      // it's object.     view_data = json.parse(view_data);   }   res.render('template', view_data); }); 

and within template.jade

h1 #{name} pre= status p #{playercount} players online 

the data can json object, if have response returned text, can use json.parse() turn json object.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -