[添加]因此,我的下一个问题是,当我尝试添加新的依赖项时(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
路径可防止此类恶意路径用于访问该基本路径之外的文件。
尝试添加根路径。
app.get('/', function(req, res) {
res.sendFile('index.html', { root: __dirname });
});
在.mjs文件中,我们现在没有__dirname
因此
res.sendFile('index.html', { root: '.' })
如果您信任路径,则可以选择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
我通过使用路径变量来解决这个问题。示例代码如下所示。
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 });
});
我使用下面的代码并尝试显示sitemap.xml文件
router.get('/sitemap.xml', function (req, res) {
res.sendFile('sitemap.xml', { root: '.' });
});
这可以通过另一种方式解决:
app.get("/", function(req, res){
res.send(`${process.env.PWD}/index.html`)
});
process.env.PWD
进程启动时,它将在工作目录之前。
我这样做了,现在我的应用可以正常运行了,
res.sendFile('your drive://your_subfolders//file.html');
您可以考虑在目录上使用双斜杠,例如
app.get('/',(req,res)=>{
res.sendFile('C:\\Users\\DOREEN\\Desktop\\Fitness Finder' + '/index.html')
})
文章标签:dependencies , javascript , json , node.js , socket.io
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!