I have a very simple app so I didn't think I needed a full-fledged front-end template like angular, and I didn't really want to use Jade
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
Basically I'm returning an html file, and I'm able to call other html files like
login.html
signup.html
But what confuses me is that my routes are not called. For example I send a GET request to /login but there is no "LOGIN GET" output
//Edit: even with req, res, I can delete this function below and still receive index.html
app.get('/login',function(req,res){
console.log("LOGIN GET")
res.sendFile( path.join( __dirname, 'public', 'login.html' ));
});
How is login.html sent to the client if my route isn't called?
Missing req and res parameters to the route handler. For static file you can also follow https://expressjs.com/en/starter/static-files.html
app.get('/login',function(req, res){
console.log("LOGIN GET")
res.sendFile( path.join( __dirname, 'public', 'login.html' ));
});
You should use the Express router.
var router = express.Router();
And then, you can get
router.get("/login",function(req,res){
console.log("LOGIN GET")
res.sendFile( path.join( __dirname, 'public', 'login.html' ));
});
My guess is that you're declaring the catch-all route first:
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get('/login',function(req,res){
console.log("LOGIN GET")
res.sendFile( path.join( __dirname, 'public', 'login.html' ));
});
Because /login matches *, Express will use the first route to handle requests for /login as well. Generally, with Express you need to declare more specific routes first:
app.get('/login',function(req,res){
console.log("LOGIN GET")
res.sendFile( path.join( __dirname, 'public', 'login.html' ));
});
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
The reason the other HTML files work is that you probably are using express.static() too (but not showing it).