2017-08-26 241 views
0

我想弄清楚這個問題的根源是什麼。根據這page,一切都應該正常工作,但我得到這個錯誤。node.js - TypeError:路徑必須是絕對路徑或指定根目錄res.sendFile

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

不知道爲什麼你要鏈接到有關靜態資源的頁面,當錯誤表示問題與['重新s.sendFile'](http://expressjs.com/en/4x/api.html#res.sendFile)。 – robertklep

回答

0

就試試這個:

res.sendFile('Public/views/inde.html' , { root : __dirname}); 
相關問題