node.js render and send in same time

How can i render jade and send some code after render without replace jade code by new code i sent it

here is example

var express = require('express');
var router = express.Router();
router.get('/user', function(req, res, next) {
//res.header("Content-Type", "application/json; charset=utf-8");
res.render('user', { title: 'Express' }); 




  //after render user jade i want to send some json to user
   res.contentType('application/json');
   res.send(json_code);

 });

When i run this code its replace the jade with json code ! how to solve it ?

Upvotes: 3

Views: 3517

Answers (1)

michelem
michelem

Reputation: 14590

Render the HTML page and inject the json_code to be used in a script tag:

JS:

router.get('/user', function(req, res, next) {
    var json_code = { 'hello': 'world' };
    res.render('user', { title: 'Express', data: JSON.stringify(json_code) }); 
});

HTML/JADE:

script
    var json_code = JSON.parse(#{data})

Upvotes: 2

Related Questions