2017-08-04 92 views
0

嘗試使用Node.js顯示html頁面時出現一個錯誤。我在下面提供錯誤。使用Node.js重定向到頁面時出現類型錯誤

TypeError: path must be absolute or specify root to res.sendFile 
    at ServerResponse.sendFile (/opt/lampp/htdocs/heroku/FGDP/node_modules/express/lib/response.js:404:11) 
    at /opt/lampp/htdocs/heroku/FGDP/server.js:40:6 
    at Layer.handle [as handle_request] (/opt/lampp/htdocs/heroku/FGDP/node_modules/express/lib/router/layer.js:95:5) 
    at next (/opt/lampp/htdocs/heroku/FGDP/node_modules/express/lib/router/route.js:131:13) 
    at Route.dispatch (/opt/lampp/htdocs/heroku/FGDP/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/opt/lampp/htdocs/heroku/FGDP/node_modules/express/lib/router/layer.js:95:5) 
    at /opt/lampp/htdocs/heroku/FGDP/node_modules/express/lib/router/index.js:277:22 
    at Function.process_params (/opt/lampp/htdocs/heroku/FGDP/node_modules/express/lib/router/index.js:330:12) 
    at next (/opt/lampp/htdocs/heroku/FGDP/node_modules/express/lib/router/index.js:271:10) 
    at Immediate.<anonymous> (/opt/lampp/htdocs/heroku/FGDP/node_modules/express-session/index.js:473:7) 

我在下面提供我的代碼。

var express=require('express'); 
var morgan = require('morgan'); 
var http=require('http'); 
var bodyParser= require('body-parser'); 
var methodOverride = require('method-override'); 
var mongo = require('mongojs'); 
var session = require('express-session'); 
var multer = require('multer') 
var app=module.exports=express(); 
var server=http.Server(app); 
var port=8989; 
var admin=require('./route/route.js'); 
var api=require('./api/api.js'); 
app.use(express.static(__dirname + '/public'));  // set the static files location /public/img will be /img for users 
app.use(morgan('dev'));      // log every request to the console 
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded 
app.use(bodyParser.json()) // parse application/json 
app.use(methodOverride());     // simulate DELETE and PUT 
app.use(session({secret: 'FGDPlexel',resave: true,saveUninitialized: true})); 
app.get('/',function(req,res){ 
    res.sendFile(__dirname + '/index.html'); 
}) 
app.get('/api/users/reset',function(req,res){ 
    res.sendFile('views/reset.html'); 
}) 
server.listen(port); 

當我輸入網址http://localhost:8989/api/users/reset?id=5981b48654d471000459208e我收到上述錯誤。我的文件夾結構如下。

fgdp(root folder) 
-> public 
    ->views 
    ->controller 

server.js 

我的HTML頁面存在於views文件夾中。

+0

請檢查是否有幫助https://stackoverflow.com/questions/25463423/res-sendfile-absolute-path –

+0

我想,在這裏你可以找到答案:https://開頭計算器.COM /問題/ 25463423/RES-sendfile的絕對路徑 –

回答

1

你沒有指定絕對路徑,請解決它。修改你的路由請求爲

app.get('/',function(req,res){ 
    res.sendFile(path.join(__dirname, '/public/views', 'index.html')); 
}); 
app.get('/api/users/reset',function(req,res){ 
    res.sendFile(path.join(__dirname, '/public/views', 'reset.html')); 
}) 
0

控制檯日誌__dirname + '/index.html'檢查index.hmtl的路徑是否正確。

其次,res.sendFile('views/reset.html');這不起作用。它清楚地表明使用絕對路徑。 views/reset.html是一個相對路徑。使用path.resolve__dirname獲得絕對路徑。

相關問題