简体   繁体   English

路由时 express.js 不显示 console.log 消息

[英]express.js not showing console.log message when routing

Note: I am very new to express注:我很新来表达

var express = require('express');
var app = express();

app.get('/', function(req, res) {
   res.send('id: ' + req.params.id + ' and name: ' + req.params.name);
});
var things = require('./things/things.js');

//both index.js and things.js should be in same directory
app.use('/things', things);
//Simple request time logger
app.use('/',function(req, res, next){
   console.log("A new request received at " + Date.now());

   //This function call is very important. It tells that more processing is
   //required for the current request and is in the next middleware
   //function/route handler.
   next();
});

app.listen(3000);

I am learning about middleware functions and am trying to show a console.log message when I go to localhost:3000, but nothing shows up in my console, what am I missing here?我正在学习中间件功能,并试图在访问 localhost:3000 时显示 console.log 消息,但我的控制台中没有显示任何内容,我在这里错过了什么?

The problem is that Express passes requests to both middleware and route handlers in order of their declaration.问题在于 Express 将请求按其声明的顺序传递给中间件和路由处理程序。 If any of them are able to handle the request (by sending back a response), any other matching middleware or route handlers that got declared later won't get called.如果它们中的任何一个能够处理请求(通过发回响应),则不会调用稍后声明的任何其他匹配的中间件或路由处理程序。

That's what's happening in your situation, where your middleware is declared after the route handlers.这就是在您的情况下发生的情况,您的中间件是路由处理程序之后声明的。

Try moving your middleware to the front:尝试将中间件移到前面:

app.use('/',function(req, res, next){
   console.log("A new request received at " + Date.now());
   next();
});

app.get('/', function(req, res) {
   res.send('id: ' + req.params.id + ' and name: ' + req.params.name);
});

var things = require('./things/things.js');

app.use('/things', things);

First, you need to check the file structure.首先,您需要检查文件结构。 If index.js and things.js are in the same directory then you need to change the require function to var things = require('./things.js');如果 index.js 和 things.js 在同一个目录下,那么你需要将 require 函数更改为var things = require('./things.js');

Next, verify that you are looking in the correct place, the console.log() message will appear in the terminal window where you loaded the express server, not in the console in your web browser.接下来,验证您是否在正确的位置查找, console.log()消息将出现在您加载 express 服务器的终端窗口中,而不是 Web 浏览器的控制台中。

The correct 'id' and 'name' of the parameters in get is like this get中参数的正确'id'和'name'是这样的

app.get('/:id/:name', function(req, res) {
   res.send('id: ' + req.params.id + ' and name: ' + req.params.name);
});

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.控制台模块提供了一个简单的调试控制台,类似于 Web 浏览器提供的 JavaScript 控制台机制。 https://nodejs.org/api/console.html https://nodejs.org/api/console.html

Node Server generated console.log message in our terminal logs (Not on the browser).节点服务器在我们的终端日志中生成了 console.log 消息(不在浏览器上)。

https://expressjs.com/en/starter/hello-world.html https://expressjs.com/en/starter/hello-world.html

在此处输入图片说明

Related: https://www.twilio.com/blog/guide-node-js-logging相关: https : //www.twilio.com/blog/guide-node-js-logging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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