gpt4 book ai didi

javascript - 如何在路由中呈现回调 JSON 响应

转载 作者:搜寻专家 更新时间:2023-10-31 23:47:31 27 4
gpt4 key购买 nike

我想将 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com