1

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.

kibowki
  • 4,206
  • 16
  • 48
  • 74

2 Answers2

1

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

Community
  • 1
  • 1
Meeker
  • 5,979
  • 2
  • 20
  • 38
  • what if alonf with request I want to log all resopnse , i added a response console logger function in http.js file after 'router' it seems to work but for 404 i function still logs 200 ok :( – amIT Jun 20 '16 at 04:19
0

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.

Prem popatia
  • 321
  • 3
  • 14