[英]Nodejs Express Log Every Call Made
Is there a way to just log every call made to my nodejs server. 有没有一种方法可以只记录对我的nodejs服务器的每次调用。 Literally just the URL of the call. 从字面上看只是呼叫的URL。 I know this is possible because I saw a super quick example in a tutorial once but I have no idea where I saw it. 我知道这是可能的,因为我曾经在一个教程中看到一个超级快速的示例,但是我不知道在哪里看到它。
Here is a crappy example of what I am looking for: 这是我要寻找的一个example脚的例子:
app.listen(port, (call)=>{
console.log(call);
console.log(`Server running on http://localhost:${port}...`);
})
Is there a way to just log every call made to my nodejs server? 有没有一种方法可以只记录对我的nodejs服务器的每次调用?
There you go: https://github.com/expressjs/morgan 你去了: https : //github.com/expressjs/morgan
Simple app that will log all request in the Apache combined format to STDOUT 简单的应用程序,它将以Apache组合格式将所有请求记录到STDOUT
var express = require('express')
var morgan = require('morgan')
var app = express()
app.use(morgan('combined'))
app.get('/', function (req, res) {
res.send('hello, world!')
})
Since the morgan
answer has been posted, if you plan on some custom-weird-logging-with-some-aditional-logic you can always create your own middleware: 由于已经发布了morgan
答案,因此,如果您计划使用一些常规逻辑进行自定义怪异日志记录,则始终可以创建自己的中间件:
var express = require('express') var app = express() /** * Custom middleware with options */ var myUrlLogger = (upperCase)=>{ if( typeof uppercase !== 'boolean' ){ upperCase = true; } return (req,res,next) =>{ console.log('Logging:', (upperCase ? req.url.toUpperCase() : req.url.toLowerCase())); next(); } } // Set the middleware before your routes app.use(myUrlLogger(false)); app.get('/', function (req, res) { res.send('hello, world!') })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.