我工作的平均棧的應用程序,而我試圖創建幾個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