我是 Node.js 新手,我遇到了一些小问题。我的 api 正在返回 JSon,但我无法在数据或响应中看到 JSon。
router.get('/search/:id', function(req, res){client.get("http://localhost:3000/api/search/5600678e1c76b4680e0d6544", function(data, response){
console.log(data);
console.log(response);
res.render('test', {test: data, user : req.user , title : 'Home'});
});
});
最佳答案
编辑:我理解你错了,另一种使用 npm 模块 request
var request = require('request');
router.get('/search/:id', function(req, res) {
request('http://localhost:3000/api/search/5600678e1c76b4680e0d6544', function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
var data = body;
res.render('test', {
test: data,
user: req.user,
title: 'Home'
});
} else {
res.end('Error: ' + error);
}
});
});
如果这不起作用,则错误可能出在 localhost:3000
上,未返回任何内容。
关于javascript - Node.js 使用来自 Api 的 node-rest-client 显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32727088/