pradeep murugan
pradeep murugan

Reputation: 675

Error: ENOENT: no such file or directory, stat '/public/main.html' at Error (native)

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:

Upvotes: 37

Views: 184546

Answers (15)

Saurabh Verma
Saurabh Verma

Reputation: 1

l was also face same kind of problem. And did all the above solution but nothing has happened. Then I deleted my public, index.html file and recreate it and use this code:

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

Upvotes: 0

zemil
zemil

Reputation: 5066

For my case I used nestjs with ServeStaticModule and it helps to add serveRoot option:

ServeStaticModule.forRoot({
    // play with options below
    rootPath: path.resolve(`./src/assets`), // I prefer to use resolve in your case it can be better to use path.join
    serveRoot: '/assets',
})

So then I found files on: http://localhost:3000/assets/1.png

Other options : ServeStaticModuleOptions

Upvotes: 1

sammyhawkrad
sammyhawkrad

Reputation: 111

__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'});
});

Upvotes: 8

jbobbins
jbobbins

Reputation: 1291

I had the same problem. It appears that my client code was somehow not "in sync" with my server code (I'm not too familiar with the inner workings of node/express/angular and how they affect each other so "in sync" is the best I can do). I had only rebuilt my server code (after having added one nodejs library and updated another, among other server code changes) with this:

npm run build.server (for my project this only builds server side. I run this sometimes when I'm testing server changes because rebuilding the client code as well, without a watch, can take a long time)

Once I rebuilt the client code this issue was resolved:

npm run build -- dev (for my project this builds everything, server and client code)

Upvotes: 1

pranav rai
pranav rai

Reputation: 11

server.js


const express = require('express');
const app = express();

app.use(express.static(__dirname + '/dist/projectName'));

app.post('/*', function(req, res){
    res.sendFile(__dirname +  '/dist/projectName/index.html');
});
app.listen(4200);

Upvotes: 1

pbhadu21
pbhadu21

Reputation: 51

Just use this : res.sendFile(__dirname + '/main.html'); It'll definitely work. :)

Upvotes: 1

Guy Korland
Guy Korland

Reputation: 9568

res.sendfile('/public/main.html');

should be changed to

res.sendfile('./public/main.html');

Upvotes: 2

akshaymittal143
akshaymittal143

Reputation: 139

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'));
});

Upvotes: 3

Andresa Prates
Andresa Prates

Reputation: 19

my server.js

const express = require('express');
const app = express();

app.use(express.static(__dirname + '/dist/projectName'));

app.get('/*', function(req, res){
    res.sendFile(__dirname +  '/dist/projectName/index.html');
});
app.listen(4200);

Upvotes: -1

Bernard Saucier
Bernard Saucier

Reputation: 2270

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.

Upvotes: 17

Shubham joshi
Shubham joshi

Reputation: 91

For me using the "." in the path didn't work, instead, I tweaked it as:

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

Upvotes: 5

Niloo
Niloo

Reputation: 29

This solution works for me:

res.sendfile('./main.html');

Upvotes: 2

Santosh
Santosh

Reputation: 660

Though OP's cause is different for this error I got the same error due to a different reason so I am posting this for other's who come here.

I had a server-side shell script that was changing the current directory. Since we use relative paths in sendfile I started seeing this error after this script would run. This shell script was being run by Node.

Upvotes: 0

jared thomas
jared thomas

Reputation: 11

I had the same problem. After emailing heroku, mine was a case sensitivity problem. One of my files was in all caps, and i had to make adjustments from there.

Upvotes: 0

vkstack
vkstack

Reputation: 1644

You missed the dot. Keep in mind relative directory is

res.sendfile('./public/main.html');

Upvotes: 3

Related Questions