Anna Vos
Anna Vos

Reputation: 62

Suppress automatic routing to index.html

I have set up a server using Node & Express. All routes work, except GET /. Instead of showing the file I send, it always shows index.html (which is also in the public folder). If I rename index.html to something else or remove it, my GET / routing does work.

const publicPath = path.join(__dirname, '../public');

app.use(express.static(publicPath));

app.get('/', (req, res) => {
    res.sendFile(publicPath + '/login-register.html');
});

Is there a way to suppress the automatic rendering of index.html?

EDIT: Might be useful to note I get no errors, neither in my console nor Chrome dev tools.

Upvotes: 0

Views: 40

Answers (1)

Tsvetan Ganev
Tsvetan Ganev

Reputation: 8856

This happens because the static middleware matches the / route since you have an index.html file in the directory and sends that file to the browser. The second middleware function never runs because the / route was already matched. If you switch the order of the middleware functions' declaration, it should work in the way you expect.

app.get('/', (req, res) => {
  res.sendFile(publicPath + '/login-register.html');
});

app.use(express.static(publicPath));

Upvotes: 2

Related Questions