作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想将 JSON 响应 body
传递到我的路由 index.js 中的渲染函数
这就是我目前所拥有的,我无法理解如何构建它,以便可以在 res.render
中访问 body
router.get('/', function(req, res, next) {
var request = require('request')
request.post('https://getpocket.com/v3/get', {
headers: {'content-type':'application/json'},
body: JSON.stringify({
consumer_key:'...',
access_token:'...',
tag: 'nodejs'
})
}, function (err, res, body) {
// how to pass body to render?
})
res.render('index', {
title: 'Express',
data: body
});
});
最佳答案
你很接近。试试这个
router.get('/', function(req, res, next) {
var request = require('request')
request.post('https://getpocket.com/v3/get', {
headers: {'content-type':'application/json'},
body: JSON.stringify({
consumer_key:'...',
access_token:'...',
tag: 'nodejs'
})
}, function (err, response, body) {// notice i changed res to response, to differentiate between express response object and request's response
res.render('index', {
title: 'Express',
data: body
});
})
});
关于javascript - 如何在路由中呈现回调 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33082905/
我是一名优秀的程序员,十分优秀!