node.js TypeError:path必须是绝对的,或者指定root to res.sendFile [解析JSON失败] [英] node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

查看:1983
本文介绍了node.js TypeError:path必须是绝对的,或者指定root to res.sendFile [解析JSON失败]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

(不知道如果这是允许在堆栈溢出,有人请让我知道,如果它不是)

(Not sure if this is allowed on Stack overflow, someone please let me know if its not)

所以我一直在试图弄清楚为什么这个错误已经返回。所有文件(HTML,JSON,JS)都位于我桌面上的同一个文件夹中。我正在使用node.js和socket.io
(新的网页开发者,请不要太苛刻:p)

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 (New-ish web developer, please don't be too harsh :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)


推荐答案

错误很清楚,您需要在配置对象中指定一个绝对(而不是相对)路径和/或设置 C $ C> 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 });

指定根 .. (例如 ..) /../../../../../ etc / passwd )设置路径可以防止这种恶意路径用于访问该基本路径之外的文件。

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必须是绝对的,或者指定root to res.sendFile [解析JSON失败]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆