[英]Getting type error while redirecting to the page using Node.js
I am getting one error while trying to display the html page using Node.js. 尝试使用Node.js显示html页面时出现一个错误。 I am providing error below. 我在下面提供错误。
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)
I am providing my code below. 我在下面提供我的代码。
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);
When I am typing the url http://localhost:8989/api/users/reset?id=5981b48654d471000459208e
I am getting the above error. 当我输入网址http://localhost:8989/api/users/reset?id=5981b48654d471000459208e
时,出现上述错误。 My folder structure is given below. 我的文件夹结构如下。
fgdp(root folder)
-> public
->views
->controller
server.js
My HTML page is present inside the views
folder. 我的HTML页面位于views
文件夹内。
you are not specifying the absolute path, please resolve it. 您未指定绝对路径,请解决该问题。 Modify your route request as 将您的路线请求修改为
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'));
})
Console log __dirname + '/index.html'
to check if the path to index.hmtl is correct or not. 控制台日志__dirname + '/index.html'
检查index.hmtl的路径是否正确。
Secondly, res.sendFile('views/reset.html');
其次, res.sendFile('views/reset.html');
this won't work. 这行不通。 It clearly says use absolute path. 它明确表示使用绝对路径。 views/reset.html
is a relative path. views/reset.html
是相对路径。 use path.resolve with __dirname
to get absolute path. 使用path.resolve与__dirname
获得绝对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.