Error: ENOENT: no such file or directory, stat '/public/main.html' at Error (native)
Solution 1
Since both the server and the index file are INSIDE the "public" directory, you can simply use :
res.sendfile('./main.html');
To answer the question in the comments : In Express 4.x, the sendfile
method was replaced by the sendFile
method (all lowercase -> camelCase). Probably just an oversight in early versions, that got fixed in the latter.
Solution 2
For me using the "." in the path didn't work, instead, I tweaked it as:
res.sendFile(__dirname + '/public/main.html');
Solution 3
I had a similar issue when I referred to dist
folder.
the relative path to index.html was:
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/project-name/index.html'));
});
Solution 4
__dirname has been deprecated and the suggested solutions may not work currently (August 2021). In Express 4.x this has been simplified and you wouldn't need to use 'path' module, you just have to specify a root option like this:
app.get('*', (req, res) => {
res.sendFile('main.html', {root: 'public'});
});
Solution 5
You missed the dot. Keep in mind relative directory is
res.sendfile('./public/main.html');
pradeep murugan
Updated on August 17, 2021Comments
-
pradeep murugan over 2 years
This is my
server.js
file:var express = require('express'), app = express(); app .use(express.static('./public')) .get('*',function (req,res) { res.sendfile('/public/main.html'); }) .listen(3000);
This is my
main.html
:<!DOCTYPE html> <html> <head> <titel>Contacts</titel> <base href'/'> </head> <body> <div class="container"> <div class="page-header"> <h1>Contatcs</h1> </div> </div> </body> </html>
And the folder structure: