問題描述
[添加]所以我的下一個問題是,當我嘗試添加新的依賴項時(npm install --save socket.io).JSON 文件也是有效的.我收到此錯誤:解析json失敗
[add] So my next problem is that when i try adding a new dependence (npm install --save socket.io). The JSON file is also valid. I get this error: Failed to parse json
npm ERR! Unexpected string
npm ERR! File: /Users/John/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse
所以我一直試圖弄清楚為什么這個錯誤一直在返回.所有文件(HTML、JSON、JS)都在我桌面上的同一個文件夾中.我正在使用 node.js 和 socket.io
So I've been trying to figure out why this error has been returning. All of the files (HTML,JSON,JS) are inside the same folder on my desktop. I'm using node.js and socket.io
這是我的 JS 文件:
This is my JS file:
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.sendFile('index.html');
});
http.listen(3000,function(){
console.log('listening on : 3000');
});
這是返回的內容:
MacBook-Pro:~ John$ node /Users/John/Desktop/Chatapp/index.js
listening on : 3000
TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (/Users/John/node_modules/express/lib/response.js:389:11)
at /Users/John/Desktop/Chatapp/index.js:5:7
at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
at next (/Users/John/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/John/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
at /Users/John/node_modules/express/lib/router/index.js:234:24
at Function.proto.process_params (/Users/John/node_modules/express/lib/router/index.js:312:12)
at /Users/John/node_modules/express/lib/router/index.js:228:12
at Function.match_layer (/Users/John/node_modules/express/lib/router/index.js:295:3)
TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (/Users/John/node_modules/express/lib/response.js:389:11)
at /Users/John/Desktop/Chatapp/index.js:5:7
at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
at next (/Users/John/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/Users/John/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
at /Users/John/node_modules/express/lib/router/index.js:234:24
at Function.proto.process_params (/Users/John/node_modules/express/lib/router/index.js:312:12)
at /Users/John/node_modules/express/lib/router/index.js:228:12
at Function.match_layer (/Users/John/node_modules/express/lib/router/index.js:295:3)
推薦答案
錯誤很明顯,你需要指定一個絕對(而不是相對)路徑和/或在配置中設置 root
res.sendFile()
的對象.例子:
The error is pretty clear, you need to specify an absolute (instead of relative) path and/or set root
in the config object for res.sendFile()
. Examples:
// assuming index.html is in the same directory as this script
res.sendFile(__dirname + '/index.html');
或指定一個根(用作res.sendFile()
的第一個參數的基本路徑:
or specify a root (which is used as the base path for the first argument to res.sendFile()
:
res.sendFile('index.html', { root: __dirname });
當您傳遞用戶生成的文件路徑時,指定 root
路徑更有用,該路徑可能包含諸如 ..
之類的格式錯誤/惡意部分(例如 ../../../../../../etc/passwd
).設置 root
路徑可防止此類惡意路徑被用于訪問該基本路徑之外的文件.
Specifying the root
path is more useful when you're passing a user-generated file path which could potentially contain malformed/malicious parts like ..
(e.g. ../../../../../../etc/passwd
). Setting the root
path prevents such malicious paths from being used to access files outside of that base path.
這篇關于node.js TypeError: path must be absolute or specified root to res.sendFile [failed to parse JSON]的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!