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

时间:2014-12-02 18:01:23

标签: javascript node.js sails.js

正如标题所说,我如何将所有请求记录到我的Sails.js服务器?我的日志配置中是否需要更改某些内容?我只想查看发送到我服务器的每个GET,POST等的列表。

2 个答案:

答案 0 :(得分:1)

您可能希望将midwhere用于express。

签出config / http.js(sails 10.5),他们有一个注释掉的例子。

如果由于某种原因你的帆的版本没有那个例子,这里是一个pastebin http://pastebin.com/xhUdFY2Z

否则这些也应该有所帮助。

Node.js : How to do something on all HTTP requests in Express? https://github.com/expressjs/morgan

答案 1 :(得分:0)

对于Sails v1.x,

您可以在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()
    }
  })(),
}}

此中间件的调用顺序与放置在顺序数组中的顺序相同。