Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my node.js express code not call console.log()?

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;
like image 677
wannabe Avatar asked Feb 03 '17 06:02

wannabe


People also ask

Can you console log in node JS?

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.

How do I enable logging in node JS?

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.

Is console log () added to execution stack?

It's a function call. It goes to the call stack.

How do I open the console in node JS?

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.

How do I run a JavaScript console log?

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(" ");


2 Answers

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.

like image 73
Suren Srapyan Avatar answered Sep 20 '22 08:09

Suren Srapyan


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.

like image 22
jack blank Avatar answered Sep 18 '22 08:09

jack blank