Javascript node.js TypeError:路径必须为绝对路径或指定res.sendFile[解析JSON失败]的根目录
[加] 所以我的下一个问题是,当我尝试添加一个新的依赖项时(npm安装--save socket.io)。JSON文件也是有效的。我得到这个错误: 无法分析jsonJavascript node.js TypeError:路径必须为绝对路径或指定res.sendFile[解析JSON失败]的根目录,javascript,json,node.js,socket.io,dependencies,Javascript,Json,Node.js,Socket.io,Dependencies,[加] 所以我的下一个问题是,当我尝试添加一个新的依赖项时(npm安装--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 Java
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)
尝试添加根路径
app.get('/', function(req, res) {
res.sendFile('index.html', { root: __dirname });
});
错误很明显,您需要在
res.sendFile()
的配置对象中指定绝对(而不是相对)路径和/或设置root
。示例:
// assuming index.html is in the same directory as this script
res.sendFile(__dirname + '/index.html');
或者指定根目录(用作res.sendFile()
的第一个参数的基本路径):
在传递用户生成的文件路径时,指定
根路径更有用,该路径可能包含格式错误/恶意的部分,如。
(例如。/../../../../../../../etc/passwd
)。设置根路径
可防止此类恶意路径被用于访问该基本路径以外的文件。如果您信任该路径,则path.resolve是一个选项:
var path = require('path');
// All other routes should redirect to the index.html
app.route('/*')
.get(function(req, res) {
res.sendFile(path.resolve(app.get('appPath') + '/index.html'));
});
这个错误非常简单。最可能的原因是index.html文件不在根目录中
或者,如果它位于根目录中,则相对引用不起作用
因此,您需要告诉服务器文件的确切位置。
这可以通过在NodeJs中使用dirname方法来实现。
只需将您的代码替换为以下代码:
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
确保在主页前添加斜杠“/”符号。否则,路径将变为:rootDirectoryindex.html
尽管您希望它是:rootDirectory/index.html,但这可以通过另一种方式解决:
app.get("/", function(req, res){
res.send(`${process.env.PWD}/index.html`)
});
process.env.PWD
将在进程启动时预先设置工作目录。在.mjs文件中,我们目前没有\uuu dirname
因此
你可以考虑在你的目录上使用双斜线(如G.P/P>)
app.get('/',(req,res)=>{
res.sendFile('C:\\Users\\DOREEN\\Desktop\\Fitness Finder'+'/index.html'))
})
我使用path变量解决了这个问题。示例代码如下所示
var path = require("path");
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname + '/index.html'));
})
如果您正在根目录上工作,那么可以使用这种方法
res.sendFile(__dirname + '/FOLDER_IN_ROOT_DIRECTORY/index.html');
但是如果您使用的是文件夹中的Routes,比如说/Routes/someRoute.js
,那么您将需要执行类似的操作
const path = require("path");
...
route.get("/some_route", (req, res) => {
res.sendFile(path.resolve('FOLDER_IN_ROOT_DIRECTORY/index.html')
});
在带有图标相对路径的Typescript中:
import path from 'path';
route.get('/favicon.ico', (_req, res) => res.sendFile(path.join(__dirname, '../static/myicon.png')));
它将重定向到localhost:8080调用上的index.html
app.get('/',function(req,res){
res.sendFile('index.html', { root: __dirname });
});
我这样做了,现在我的应用程序运行正常
res.sendFile('your drive://your_subfolders//file.html');
我使用了下面的代码并尝试显示sitemap.xml文件
router.get('/sitemap.xml', function (req, res) {
res.sendFile('sitemap.xml', { root: '.' });
});
app.js中的这个配置对我来说很好:
//production mode
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (req, res) => {
res.sendFile(path.join((__dirname + "/client/build/index.html")));
});
}
//build mode
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/client/public/index.html"));
});
指定根目录为目录的最佳方法是什么?@SuperUberDuper您的意思是像path.resolve(\uu dirname,…/public')
?这将解析到脚本父目录的“public”子目录。酷!这会在将来将此值永久存储在\uu dirname中吗?嗨,我尝试了以下res.sendFile(path.resolve)(uuDirName+/index.html','../'))但获取消息:无法获取/@SuperUberDuper这是一个很好的解决方案,因为我的要求是在获取通过uuuDirName.html的路径后返回,所以我给出了res.sendFile('index.html',{root:'./public/views'));对于handelbars中的SITEMAP.XML,这是正确的解决方案。非常感谢muchIt对文件位置进行硬编码的错误做法。如果将应用程序部署到不同的计算机上,文件路径很可能会不同
router.get('/sitemap.xml', function (req, res) {
res.sendFile('sitemap.xml', { root: '.' });
});
//production mode
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "client/build")));
app.get("*", (req, res) => {
res.sendFile(path.join((__dirname + "/client/build/index.html")));
});
}
//build mode
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/client/public/index.html"));
});