ENOENT: no such file or directory, stat ERROR with Express Middleware

user6758349

This seems to be a common error with file paths but my problem is a but more strange because code worked fine yesterday but not today (and I did not change any code). My folder directory is quite simple:

-node_modules
-public
    -css
    -js
    control_panel.html
    index.html
app.js
packages.json

enter image description here

and I am using an Express middleware inside app.js to help render files.

var express = require("express");
var app = express();

app.use(express.static("public"));

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

app.get('/control_panel', function get(req, res) {
    res.sendFile('/control_panel.html');
});

When I try to open index.html in the browser, there is no problem, everything works as expected. When I try to open control_panel.html in the browser, however, I get Error: ENOENT: no such file or directory, stat '/control_panel.html' at Error (native)

What is causing the problem?

jfriend00

A number of relevant points based on your situation:

  1. All static assets (html files, images, CSS files, client-side script files, etc...) should be serviced automatically with an appropriate express.static(...) statement. You should not be creating individual routes for static resources.

  2. To make express.static() work properly, you must locate all your static resources in a directory hierarchy that contains only files meant to be public.

  3. Your private server-side files such as app.js should not be in that public directory hierarchy. They should be elsewhere.

  4. Your path to express.static() is not correct.

  5. Your path for res.sendFile() is not correct.

What I would suggest you do to fix things is the following:

  1. Move app.js out of the public directory. It needs to be in a private directory. I'd suggest the public directory be a sub-directory from where app.js is located.
  2. Then, your express.static() in app.js will work property to serve your static HTML fiels.
  3. Then, you can remove the two routes you have for index.html and control_panel.html because express.static() should be serving them.

Here's one hierarchy that would work:

server
    app.js
    public
        index.html
        control_panel.html

And, an app.js like this:

var express = require("express");
var app = express();

// serve static files found in the public sub-directory automatically
app.use(express.static("public")); 

app.listen(80);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Error: ENOENT: no such file or directory, scandir

How to resolve Nodejs: Error: ENOENT: no such file or directory

Error: open failed: ENOENT (No such file or directory)

Error: ENOENT, no such file or directory Node JS

Node, Angular Error: ENOENT: no such file or directory

Heroku ENOENT: no such file or directory, stat '/app/build/index.html'

Error: ENOENT: no such file or directory, open './views/s.ejs' Nodejs Express

Error: ENOENT: no such file or directory, unlink

npm install fail -- error message: enoent ENOENT: no such file or directory

Error: ENOENT: no such file or directory,

enoent ENOENT: no such file or directory,

Angular 4 - ERROR in ENOENT: no such file or directory, stat

Google Cloud Function - Error: ENOENT: no such file or directory

Heroku - Error: ENOENT: no such file or directory, stat '/app/dist/index.html'

Heroku Error: ENOENT: no such file or directory, open '.env'

nodemailer-express-handlebars | Error: ENOENT: no such file or directory for template

React.js: Heroku: Error: ENOENT: no such file or directory, stat '/app/src/client/build/index.html'

HEROKU Error: ENOENT: no such file or directory, stat '/app/distpublic/index.html'

Error: ENOENT, no such file or directory - PhoneGap

Eclipse Error: ENOENT, no such file or directory

Running Grunt Express Server causes Error: ENOENT: no such file or directory, open 'static/test.json'

Use multiples DefaultLayout (express-handlebars) in Nodejs: ( Error: ENOENT: no such file or directory )

Express ENOENT: no such file or directory, stat '/index.html'

Error: ENOENT: no such file or directory, stat '/home/dylan/.steampath'

Error: ENOENT: no such file or directory, stat'home/container/the args where the avatar is/ (discord)

Error: ENOENT: no such file or directory, access

Error: ENOENT: no such file or directory NodeJS

Github CI run getting failed without change from 15 June ERROR: Error: ENOENT: no such file or directory, stat '/var/folders/sp/m