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;
log() function from console class of Node. js is used to display the messages on the console. It prints to stdout with newline. Parameter: This function contains multiple parameters which are to be printed.
Logging in Node. By default, it will not produce any output. To enable this logger, you have run your application with a special environment variable, called DEBUG . Once you do that, the debug module will come to life and will start producing log events for stdout.
It's a function call. It goes to the call stack.
To launch the REPL (Node shell), open command prompt (in Windows) or terminal (in Mac or UNIX/Linux) and type node as shown below. It will change the prompt to > in Windows and MAC. You can now test pretty much any Node.
The console.log() is a function in JavaScript that is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.log(" ");
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With