node.js TypeError: path 必须是绝对路径或指定 root 到 res.sendFile [解析 JSON 失败]

[添加]所以我的下一个问题是,当我尝试添加新的依赖项(npm安装--保存 socket.io)。JSON 文件也有效。我收到此错误:无法解析 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

这是我的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)

答案 1

错误非常清楚,您需要在 的配置对象中指定绝对(而不是相对)路径和/或设置。例子:rootres.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..../../../../../../etc/passwdroot


答案 2

在.mjs文件中,我们现在没有__dirname

因此

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