Reputation: 37038
I have the following code:
const app = express();
app.use(express.static(path.resolve('../frontend/dist')));
const server = http.createServer(app);
app.get('/', (req, res) => {
console.log('received request');
res.sendFile(path.resolve('../frontend/dist/index.html'));
});
If I comment out the app.get
handler, index.html is served at localhost:3000
anyway, apparently due to the second line. The get handler is not actually executing - received request
is never printed in the console.
But without the second line, it can't serve static assets and JS & CSS fail to load. What am I doing wrong here? Why is the static asset config causing the route to be ignored?
Upvotes: 1
Views: 37
Reputation: 707298
express.static()
is a generic route handler. It tries to match incoming requests to specific files at a target location in your file system. One of the special features it has is that if it gets a request for /
it will look for index.html
in the target directory.
You can see the feature described in the doc and, in fact, there's an option { index: false}
that can turn the feature off if you don't want it to serve index.html
automatically.
Upvotes: 1