<legend id='8C4Q2'><style id='8C4Q2'><dir id='8C4Q2'><q id='8C4Q2'></q></dir></style></legend>

    <i id='8C4Q2'><tr id='8C4Q2'><dt id='8C4Q2'><q id='8C4Q2'><span id='8C4Q2'><b id='8C4Q2'><form id='8C4Q2'><ins id='8C4Q2'></ins><ul id='8C4Q2'></ul><sub id='8C4Q2'></sub></form><legend id='8C4Q2'></legend><bdo id='8C4Q2'><pre id='8C4Q2'><center id='8C4Q2'></center></pre></bdo></b><th id='8C4Q2'></th></span></q></dt></tr></i><div id='8C4Q2'><tfoot id='8C4Q2'></tfoot><dl id='8C4Q2'><fieldset id='8C4Q2'></fieldset></dl></div>
  • <tfoot id='8C4Q2'></tfoot>
  • <small id='8C4Q2'></small><noframes id='8C4Q2'>

        <bdo id='8C4Q2'></bdo><ul id='8C4Q2'></ul>

        node.js TypeError: path must be absolute or specified root to

        时间:2023-10-13
        <legend id='XSkUu'><style id='XSkUu'><dir id='XSkUu'><q id='XSkUu'></q></dir></style></legend>
        • <bdo id='XSkUu'></bdo><ul id='XSkUu'></ul>

              <tbody id='XSkUu'></tbody>

            1. <small id='XSkUu'></small><noframes id='XSkUu'>

              • <tfoot id='XSkUu'></tfoot>
                <i id='XSkUu'><tr id='XSkUu'><dt id='XSkUu'><q id='XSkUu'><span id='XSkUu'><b id='XSkUu'><form id='XSkUu'><ins id='XSkUu'></ins><ul id='XSkUu'></ul><sub id='XSkUu'></sub></form><legend id='XSkUu'></legend><bdo id='XSkUu'><pre id='XSkUu'><center id='XSkUu'></center></pre></bdo></b><th id='XSkUu'></th></span></q></dt></tr></i><div id='XSkUu'><tfoot id='XSkUu'></tfoot><dl id='XSkUu'><fieldset id='XSkUu'></fieldset></dl></div>
                  本文介绍了node.js TypeError: path must be absolute or specified root to res.sendFile [failed to parse 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 
                  

                  所以我一直试图弄清楚为什么这个错误一直在返回.所有文件(HTML、JSON、JS)都在我桌面上的同一个文件夹中.我正在使用 node.js 和 socket.io

                  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

                  这是我的 JS 文件:

                  This is my JS file:

                  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)
                  

                  推荐答案

                  错误很明显,你需要指定一个绝对(而不是相对)路径和/或在配置中设置 rootres.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 });
                  

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

                  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 must be absolute or specified root to res.sendFile [failed to parse JSON]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 XMLhttpRequest 变成函数失败:异步还是其他? 下一篇:我应该链接到 Google API 的 JS 库云吗?

                  相关文章

                  最新文章

                1. <tfoot id='iomRn'></tfoot><legend id='iomRn'><style id='iomRn'><dir id='iomRn'><q id='iomRn'></q></dir></style></legend>

                    <bdo id='iomRn'></bdo><ul id='iomRn'></ul>

                    <small id='iomRn'></small><noframes id='iomRn'>

                    <i id='iomRn'><tr id='iomRn'><dt id='iomRn'><q id='iomRn'><span id='iomRn'><b id='iomRn'><form id='iomRn'><ins id='iomRn'></ins><ul id='iomRn'></ul><sub id='iomRn'></sub></form><legend id='iomRn'></legend><bdo id='iomRn'><pre id='iomRn'><center id='iomRn'></center></pre></bdo></b><th id='iomRn'></th></span></q></dt></tr></i><div id='iomRn'><tfoot id='iomRn'></tfoot><dl id='iomRn'><fieldset id='iomRn'></fieldset></dl></div>