[英]How to send a response from a module passing them the request and response objects in RESTIFY
[英]Trying to send JSON response with Restify
我正在嘗試使用當前調用的API列出angel.co上具有特定標簽的所有作業
https://api.angel.co/1/tags/10/startups
然后嘗試解析它並使用restify在瀏覽器中顯示
var tagUrl = "https://api.angel.co/1/tags/10/startups"
request({
url: tagUrl,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body) // Print the json response
}
else console.log("error" + error)
})
我正在使console.log(body)-部分正常工作,但是當我嘗試將響應發送到瀏覽器時,當我嘗試將其發送給瀏覽器時,它沒有顯示任何內容
res.send('hello ' + req.params.name + body);
我應該以某種方式解析或字符串化它嗎?
編輯:這是最終代碼
function respond(req, res, next) {
var tag = req.params.tag;
var url = "http://api.angel.co/1/tags/"+tag+"/startups/?
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body) // Print the json response
res.send( req.params.name + JSON.stringify(body));
}
else console.log("error" + error)
})
使用json的stringify屬性,它將json格式轉換為字符串
JSON.stringify(body)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.