node.js TypeError:path must be absolute or specify root to res.sendFile [failed to parse JSON]问题如何解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (203)

[add]所以我的下一个问题是,当我尝试添加新的依赖(npm install --save socket.io)。JSON文件也是有效的。我得到这个错误: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(New-ish web开发人员,请不要太苛刻:p)

这是我的JS文件:

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在config对象中设置res.sendFile()。例子:

// assuming index.html is in the same directory as this script

res.sendFile(__dirname + '/index.html');

或者指定一个根(它被用作第一个参数的基本路径res.sendFile()

res.sendFile('index.html', { root: __dirname });

root当传递用户生成的文件路径时,指定路径会更加有用,该文件路径可能包含..(eg ../../../../../../etc/passwd)等格式错误/恶意的部分。设置root路径可防止这些恶意路径被用于访问该基本路径之外的文件。

用户回答回答于

尝试添加根路径。

app.get("/", function(req, res)
{
    res.sendFile("index.html", {"root": __dirname});
});

扫码关注云+社区