我已经设法向API发出GET请求,但是现在我想在Jade页面上获得响应。下面是目前为止GET请求的代码。它在routes文件夹下的index.js中。有什么想法,我怎么能得到对翡翠页面的回应?
router.get('/', function(req, res) {
var url = 'http://data.police.uk/api/forces';
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var response = JSON.parse(body)
console.log("Successful")
});
}).on('error', function(e){
console.log("Error: ", e);
})
});
module.exports = router;
在你的路由器
res.render('viewName',{"variable":"value"});
在你的视图
p #{variable}
与其尝试从头开始,你可能应该看看一些使用express的node.js示例模板。
http://blog.ijasoneverett.com/2013/03/a-sample-app-with-node-js-express-and-mongodb-part-1/这将告诉你如何设置应用程序的结构。