我正在试图弄清楚这个问题的根源是什么。根据这个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
答案 0 :(得分:0)
试试这个:
res.sendFile('Public/views/inde.html' , { root : __dirname});