I know this will be some stupid thing but I don't know why express.js is doing this.
So, I am sending a HTML file in a GET
request
const express = require("express");
require('dotenv').config()
const app = express();
const PORT = process.env.PORT || 5000;
app.use(express.static(__dirname + "/public"));
app.get("/", (req, res) => {
// console.log("Hello");
// res.sendFile(__dirname + "/public/index2.html");
})
app.listen(PORT, () => {
console.log("Server started on port: " + PORT)
})
Now I was sending index.html
instead of index2.html
but it still sends the index.html
file, even the console.log
is not being printed.
Can someone please tell me why this is happening?
Express tests the routes one at a time in order until one matches.
app.use(express.static(__dirname + "/public"));
Your very first route, which is the static route, matches!
If you want to prioritise your explicit end points over your static ones, then put the static route last.