当前位置: 首页 > 面试题库 >

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

师成弘
2023-03-14
问题内容

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

问题答案:

错误非常明显,您需要指定一个绝对路径(而不是相对路径)和/或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当您传递用户生成的文件路径时,指定路径可能会更有用,该文件路径可能包含格式不正确/恶意的部分..(例如../../../../../../etc/passwd)。设置root路径可防止此类恶意路径用于访问该基本路径之外的文件。



 类似资料:
  • [add]所以我的下一个问题是,当我尝试添加一个新的依赖项时(npm安装--save socket.io)。JSON文件也是有效的。我得到这个错误:解析json失败 所以我一直在试图弄清楚为什么这个错误会返回。所有文件(超文本标记语言、JSON、JS)都在我桌面上的同一个文件夹中。我用node.js和socket.io 这是我的JS文件: 这就是返回的内容:

  • 问题内容: 如果我做一个 然后我收到服务器控制台警告 表达已弃用:改为使用 但它在客户端工作正常。 但是当我将其更改为 我得到一个错误 TypeError:路径必须是绝对路径或将根目录指定为 并且不呈现。 我无法弄清楚绝对路径是什么。我的目录与处于同一级别。我正在使用。我也宣布 添加我的目录结构: 在此处指定的绝对路径是什么? 我正在使用Express4.x。 问题答案: 该中间件是独立的,所以用

  • 我正在将正在运行的Vue.js应用程序转换为使用Vue cli/Webpack和导入的模块。目前,我发现有一件事相当乏味,那就是准确地指定导入的相对路径。例如,,。容易犯错误。 我假设指定一个基目录或根目录并从中指定绝对路径必须足够简单,但我看不出在哪里可以这样做。例如,在标准的vue cli网页包设置中,我正在处理的代码都在“src”目录中,其中有“components”、“mixin”等。如果

  • 问题内容: 如果使用绝对路径,则无法将整个目录移动到新位置。如果使用相对路径,则无法将单个文件移动到新位置。 这里有什么解决方案?您是否设置了一个包含根路径并从那里开始的配置文件?还是您有类似的规则:永不移动文件? 我在某些项目中看到人们使用dirname( FILE )。我的意思是,为什么不简单地将其删除,因为目录名还是相对的(取决于文件所在的位置)? 问题答案: 您应该使用一个配置文件,该配置

  • 问题内容: 我有文件A的绝对路径。 我有一个从文件A的目录到文件B的相对路径。此路径可以并且将使用“ ..”以任意复杂的方式向上移动目录结构。 范例A: 示例B: 我如何结合这两者以获得 最简单的 绝对路径到文件B? 问题答案: 如果我的问题正确,那么您可以执行以下操作:

  • 问题内容: 节点中是否有类似“ path.resolve”的API?还是有什么可以做的? 例如(nodejs代码): 应该得到: 问题答案: 解决(表示用户的家)是另一回事,通常是由外壳来解决。。 如果要通过Go代码执行此操作,则可以使用该函数获取有关当前用户的详细信息,包括其主文件夹(即)。但是,您仍然必须自己替换它。 原始答案如下。 您可以使用或。 例如: 输出: 您可以使用和从路径中“删除”