Node.js - res.sendFile - 错误:ENOENT但路径正确

Ale*_*lex 2 node.js sendfile express

我正在尝试渲染一个index.html但是我得到了错误enoent,即使是正确的路径.

//folders tree
test/server.js
test/app/routes.js
test/public/views/index.html

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


//server.js
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app);
Run Code Online (Sandbox Code Playgroud)

我也试过了

res.sendFile(__dirname + '/public/views/index.html');
Run Code Online (Sandbox Code Playgroud)

如果我使用

res.sendfile('./public/views/index.html');
Run Code Online (Sandbox Code Playgroud)

然后它工作,但我看到一个警告,说sendfile已被弃用,我必须使用sendFile.

Nia*_*lJG 5

尝试添加:

 var path = require('path');
 var filePath = "./public/views/index.html"
 var resolvedPath = path.resolve(filePath);
 console.log(resolvedPath);
 return res.sendFile(resolvedPath);
Run Code Online (Sandbox Code Playgroud)

这应该清除文件路径是否符合您的预期