Reputation: 3560
I am getting one error while trying to display the html page using Node.js. 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. My folder structure is given below.
fgdp(root folder)
-> public
->views
->controller
server.js
My HTML page is present inside the views
folder.
Upvotes: 0
Views: 79
Reputation: 1844
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'));
})
Upvotes: 1
Reputation: 2528
Console log __dirname + '/index.html'
to check if the path to index.hmtl is correct or not.
Secondly, res.sendFile('views/reset.html');
this won't work. It clearly says use absolute path. views/reset.html
is a relative path. use path.resolve with __dirname
to get absolute path.
Upvotes: 0