javascript - node.js TypeError : path must be absolute or specify root to res. sendFile [无法解析JSON]

标签 javascript json node.js socket.io dependencies

[添加] 所以我的下一个问题是,当我尝试添加新的依赖项时(npm install --save 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)

最佳答案

错误很明显,您需要在 res.sendFile() 的配置对象中指定绝对(而不是相对)路径和/或设置 root .例子:

// 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/passwd)。设置 root 路径可防止此类恶意路径被用于访问该基本路径之外的文件。

关于javascript - node.js TypeError : path must be absolute or specify root to res. sendFile [无法解析JSON],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26079611/

相关文章:

javascript - 包含函数反模式的对象中的参数太多?

javascript - 我如何使输入字段和提交按钮变灰

javascript - SAPUI5/OPENUI5 : Bind string to JSON model and update bigger string automatically (it reads [object%20Object])

json - 如何在httpkit中将Content-Type设置为application/json

java - 错误 : 'ContentNegotiatingViewResolver' of Spring 3. 0.3 MVC Portlet+JSON

node.js - npm 串口获取输出以 react 组件

node.js - 在react和express项目中使用docker compose up后出错

javascript - 在 webpack (SSR) 捆绑的 express 中找不到静态文件夹

javascript - 如何使用 Excel JavaScript API office Add-IN 最大限度地提高表行添加 50K+ 行的性能

javascript - 当元素在 angularjs 中显示或隐藏时,如何在指令中获取监视函数来触发