简体   繁体   中英

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;

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.

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.

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.

除非您将 DEBUG 环境变量设置为 express,否则 express 不会将 console.log 输出到终端:* https://expressjs.com/en/guide/debugging.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM