我工作的平均栈的应用程序,而我试图创建几个API端点的角客户端可以$http.get
,用简单的JSON文件填充虚拟数据。节点/快递:尝试发送静态JSON文件API端点
这里的orders.json
文件我试图来测试它:
[
{
"order_status":"Shipped",
"order_qty":30
},
{
"order_status":"Shipped",
"order_qty":6
}
]
例如,API路线$ http.get:
apiRouter.get('/:fileName', queries.getStaticJSONFileForDevelopment);
但是,当我尝试使用快递的sendFile
方法与当地以.json文件,像orders.json
:
queries.js:
exports.getStaticJSONFile = function(req, res) {
var fileName = req.params.fileName;
console.log('path: ' + path.normalize(__dirname + '/' + fileName));
res.sendFile(path.normalize(__dirname + '/' + fileName), function(err) {
if (err) return res.send({ reason:error.toString() });
});
};
console.log
告诉我,我指着正确的文件路径,但邮递员投递此错误:
TypeError: undefined is not a function
at Object.exports.getStaticJSONFile [as handle] (path/to/queries.js:260:7)
// queries.js:260:7 points to the 's' in 'sendFile' above
然而,当我刚刚通过自身发送JSON数据:
res.send([{"order_status":"Shipped","order_qty":30},{"order_status":"Shipped","order_qty":6}]);
。终端按照您的预期呈现数据。我是否试图让sendFile方法做一些它并不意味着做的事情,或者是否有我失踪的东西?非常感谢您的任何建议!
你想与文件来回答连接或您要回应的内容类型:应用程序/ JSON?也许你想读取json文件并输出json数据? – num8er