首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >node.js TypeError:路径必须是绝对路径或指定res.sendFile的根目录[无法分析JSON]

node.js TypeError:路径必须是绝对路径或指定res.sendFile的根目录[无法分析JSON]
EN

Stack Overflow用户
提问于 2014-09-28 06:02:30
回答 15查看 207.1K关注 0票数 191

添加所以我的下一个问题是,当我尝试添加一个新的依赖项时(npm install --保存socket.io)。JSON文件也是有效的。我得到这个错误:无法解析json

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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');
});

下面是要返回的内容:

代码语言:javascript
复制
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)
EN

回答 15

Stack Overflow用户

回答已采纳

发布于 2014-09-28 06:06:19

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

代码语言:javascript
复制
// assuming index.html is in the same directory as this script

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

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

代码语言:javascript
复制
res.sendFile('index.html', { root: __dirname });

当您传递用户生成的文件路径时,指定root路径更有用,因为该路径可能包含格式错误/恶意的部分,如.. (例如../../../../../../etc/passwd)。设置root路径可防止此类恶意路径被用于访问该基本路径之外的文件。

票数 405
EN

Stack Overflow用户

发布于 2017-11-14 09:14:20

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

因此

代码语言:javascript
复制
res.sendFile('index.html', { root: '.' })
票数 29
EN

Stack Overflow用户

发布于 2014-09-28 06:05:47

尝试添加根路径。

代码语言:javascript
复制
app.get('/', function(req, res) {
    res.sendFile('index.html', { root: __dirname });
});
票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26079611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档