Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Last active May 21, 2016 00:44
Show Gist options
  • Save mikermcneil/9cbd68c95839da480e97 to your computer and use it in GitHub Desktop.
Save mikermcneil/9cbd68c95839da480e97 to your computer and use it in GitHub Desktop.
// ...
middleware: {
// Define a custom HTTP middleware fn with the key `foobar`:
foobar: function (req,res,next) { /*...*/ next(); },
// Define another couple of custom HTTP middleware fns with keys `passportInit` and `passportSession`
// (notice that this time we're using an existing middleware library from npm)
passportInit : require('passport').initialize(),
passportSession : require('passport').session(),
// Override the conventional cookie parser:
cookieParser: function (req, res, next) { /*...*/ next(); }
}
// ...
@calidion
Copy link

how to enable compress and change poweredBy?

@calidion
Copy link

figured out like this.
hope this messsage will be helpful to someone else.

    poweredBy: function(req, res, next) {
      res.setHeader('X-Powered-By', "Autobots");
      next();
    },
    compress: require('compression')(),
    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'myRequestLogger',
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      '$custom',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ]

@luislobo
Copy link

@calidion The problem of adding that in the http config, is that you will not have those headers when using sockets.
If you want to have them on sockets too, then, create a hook like this in /api/hooks/headers/index.js

/**
 * Headers Hook that adds server name and powered by to request
 * @param Object sails app
 */
module.exports = function (sails) {
  return {
    initialize: function (next) {
      sails.emit('hook:headers:loaded');
      return next();
    },
    routes: {
      before: {
        'all /*': function (req, res, next) {
          res.setHeader('X-Served-By', require('os').hostname());
          res.setHeader('X-Powered-By', 'SuperDupperApp');
          return next();
        }
      }
    }
  };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment