Reputation: 131
How come "homepage requested" never gets output to the terminal console when I run server.js
and go to localhost:8000
? index.html
renders fine
server.js
var express = require("express");
var path = require('path');
var index = require('./routes/index');
var app = express();
var port = 8000;
app.set("views", path.join(__dirname, 'views'));
app.set("view engine", "ejs");
app.engine("html", require("ejs").renderFile);
app.use(express.static(path.join(__dirname, 'client')));
app.use('/', index);
app.listen(port, function() {
console.log("server started on port", port);
});
index.js
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
console.log('homepage requested'); // never executes
res.render('index.html'); // always executes
});
module.exports = router;
Upvotes: 9
Views: 14137
Reputation: 816
In my case, I found that it was writing my console log, but then generating a lot of other messages afterwards, so I had to scroll the terminal back. At first I thought it meant the server was restarting after a file save or crash, so I didn't bother to scroll back, which is why I started searching online. This is different from the way it behaved for me in the past, where my logs were closer to the end.
Upvotes: 0
Reputation: 5195
It has to do with the way you use app.use(express.static(path.join(__dirname, 'client')));
If you take it out you should be able to see the console.log
It appears that if no mount path
in the app.use
and it sees a route with "/" (home route) it just serves up the static file from the express.static
. It doesn't continue with any consoles.
A possible workaround for getting the console.log is to do a URL check in the app.use
and that will only print out the home page.
app.use("/", (req, res, next) => {
if (req.url == "/") {
console.log("just hompage")
}
return next()
}, express.static(path.join(__dirname, "client/build")))
I think part of the reason why this is happening is that express.static
ends the response. and you can't console anything after that. I think you don't even need the app.get("/")
or router.get("/")
because it by default if there is no mount path it work on "/" route. So it sends file automatically.
I think express.static
runs on every route hit to check for static files.
Upvotes: 2
Reputation: 903
You have an index file with the same name as the route.
Where you add the route, app.use('/', index);
This won't get called if the index.html file exists and is already served up (as the file has the same name as the route).
Change the name of index.html to index2.html. Then do this
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
console.log('homepage requested'); // never executes
res.render('index2.html'); // always executes
});
module.exports = router;
Your console should now show your message.
Upvotes: -1
Reputation: 11
express won't output console.log to the terminal unless you set the DEBUG environment variable to express:* https://expressjs.com/en/guide/debugging.html
Upvotes: -2
Reputation: 68665
console.log('homepage requested')
will print the message in the terminal
, not in the browser
. If you run your server with command line node index
, and then open your page, in the terminal you will see the message.
Upvotes: 4