Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express Static Middleware serves index.html automatically

I'm not very familiar with NodeJS and started to work with it and Express. Now I'm getting the following content: I want to serve an index.html file but make some other things before that. But since I'm using app.use(express.static(__dirname + '/client/public')); a browser request does not affect the app.get("/") function. How do I solve that problem?

app.use(express.static(__dirname + '/client/src/css'));
app.use(express.static(__dirname + '/client/public'));

app.get('/', function (req, res) {
    console.log('###GET REQUEST received');
    console.log(req);
    res.sendFile(__dirname + '/index.html'); 
});

Thank you in advance!

like image 430
patreu22 Avatar asked Feb 14 '17 12:02

patreu22


2 Answers

Order matters. Put your app.get route prior to the Express.static declaration.

like image 51
Paul Avatar answered Nov 03 '22 00:11

Paul


Or, disable the indexing.

express.static(path, {index: false})

index: sends the specified directory index file; set to false to disable directory indexing.

from https://expressjs.com/en/4x/api.html#express.static

like image 28
Константин Ван Avatar answered Nov 02 '22 23:11

Константин Ван