node.js -类型错误:路径必须是绝对路径或指定res.sendFile根目录

zf9nrax1  于 2022-11-03  发布在  Node.js
关注(0)|答案(2)|浏览(148)

我试图找出这个问题的根源是什么。根据这个page,一切都应该工作正常,但我得到这个错误。

TypeError: path must be absolute or specify root to res.sendFile
at ServerResponse.sendFile (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/response.js:410:11)
at /Users/apple/Documents/JavsScript/test/server.js:13:9
at Layer.handle [as handle_request] (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/layer.js:95:5)
at /Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/index.js:335:12)
at next (/Users/apple/Documents/JavsScript/test/node_modules/express/lib/router/index.js:275:10)
at SendStream.error (/Users/apple/Documents/JavsScript/test/node_modules/serve-static/index.js:121:7)

下面是我的代码:

var host = '127.0.0.1';
var port = 8020;

var express = require('express');
var path = require('path');

var app = express();

app.use(express.static(path.join(__dirname, 'Public')))

app.get('/', function (req, res) {
    res.sendFile('index.html');
});

app.listen(port, host, function () {
    console.log('App listening on ' + host + ':' + port);
});

下面是我目录结构:

server.js
node_modules
package.json
Public  
    views
       inde.html
6l7fqoea

6l7fqoea1#

试试这个:

res.sendFile('Public/views/inde.html' , { root : __dirname});
3pmvbmvn

3pmvbmvn2#

如果您有任何问题,请与我们联系。__目录名});为我工作,只是想给予一些信任!

相关问题