简体   繁体   English

如何将所有 HTTP 请求记录到我的 Sails.js 服务器?

[英]How to log all HTTP requests to my Sails.js server?

As the title says, how can I log all requests to my Sails.js server?正如标题所说,如何将所有请求记录到我的 Sails.js 服务器? Is there something I need to change in my log configuration?我的日志配置有什么需要改变的吗? I just want to see a list of every GET, POST, etc that is sent to my server.我只想查看发送到我的服务器的每个 GET、POST 等的列表。

You would want to use middlewhere for express.您可能想要使用 middlewhere 来表达。

Checkout the the config/http.js (sails 10.5) and they have a commented out example.检查 config/http.js (sails 10.5),他们有一个注释掉的例子。

If for some reason your version of sails does not have that example, here is a pastebin http://pastebin.com/xhUdFY2Z如果由于某种原因你的帆版本没有那个例子,这里是一个 pastebin http://pastebin.com/xhUdFY2Z

Otherwise these should help as well.否则,这些也应该有所帮助。

Node.js : How to do something on all HTTP requests in Express? Node.js:如何处理 Express 中的所有 HTTP 请求? https://github.com/expressjs/morgan https://github.com/expressjs/morgan

For Sails v1.x,对于 Sails v1.x,

You can implement middleware in config/http.js file.您可以在 config/http.js 文件中实现中间件。

module.exports.http = {

middleware: {

  // default given middleware is commented by me just for concentrating on example
  // you should enable needed middlewares always

  order: [
    // 'cookieParser',
    // 'session',
    // 'bodyParser',
    // 'compress',
    // 'poweredBy',
    // 'router',
    // 'www',
    // 'favicon',
    'foobar'
  ],

  foobar: (function() {
    return function(req, res, next) {

      // here you will be having access to the Request object "req"
      let requestUrl = req.url
      let requestIpAddress = req.ip
      let requestMethod = req.method
      let requestHeader = req.header
      let requestBody = req.body

      // Now before going to return next(), you can call service and give above parameters to store in to Database or log in console.

      return next()
    }
  })(),
}}

This middleware will be called in same order in which it is placed inside order array.这个中间件将按照放置在 order 数组中的相同顺序调用。

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

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