kibowki
kibowki

Reputation: 4376

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? 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.

Upvotes: 1

Views: 1630

Answers (2)

Prem popatia
Prem popatia

Reputation: 333

For Sails v1.x,

You can implement middleware in config/http.js file.

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.

Upvotes: 0

Meeker
Meeker

Reputation: 5979

You would want to use middlewhere for express.

Checkout the the config/http.js (sails 10.5) and they have a commented out example.

If for some reason your version of sails does not have that example, here is a pastebin http://pastebin.com/xhUdFY2Z

Otherwise these should help as well.

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

Upvotes: 1

Related Questions