Alex Alex - 2 days ago 5
Node.js Question

Node.js - res.sendFile - Error: ENOENT but the path is correct

I'm learning mean stack and I'm trying to render an index.html but I get the error enoent, even with the right path.

//folders tree
test/server.js
test/app/routes.js
test/public/views/index.html

//routes.js
app.get('*', function(req, res) {
res.sendFile('views/index.html');
});


//server.js
app.use(express.static(__dirname + '/public'));
require('./app/routes')(app);


I also tried

res.sendFile(__dirname + '/public/views/index.html');


If I use

res.sendfile('./public/views/index.html');


then it works, but I see a warning that says sendfile is deprecated and I have to use sendFile

Answer

Try adding :

 var path = require('path');
 var filePath = "./public/views/index.html"
 var resolvedPath = path.resolve(filePath);
 console.log(resolvedPath);
 return res.sendFile(resolvedPath);

This should clear up whether the file path is what you expect it to be

Comments