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
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?
A number of relevant points based on your situation:
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.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.Your private server-side files such as
app.js
should not be in that public directory hierarchy. They should be elsewhere.Your path to
express.static()
is not correct.Your path for
res.sendFile()
is not correct.What I would suggest you do to fix things is the following:
public
directory. It needs to be in a private directory. I'd suggest thepublic
directory be a sub-directory from where app.js is located.express.static()
inapp.js
will work property to serve your static HTML fiels.index.html
andcontrol_panel.html
becauseexpress.static()
should be serving them.Here's one hierarchy that would work:
And, an app.js like this: