Slava Fomin II Slava Fomin II - 5 days ago 6
Javascript Question

How to disable or replace X-Powered-By header in Sails.js application

When I run Sails.js application, it adds the following HTTP header automatically to every response:

X-Powered-By: "Sails <sailsjs.org>"
.

Is it possible to disable or override it?

Answer

Yes, it's quite possible.

You will need to disable the Sails's middleware called poweredBy and also tell Express.js server not to add it's own header.

Just update your config/http.js configuration file to looks like this:

module.exports.http = {
  middleware: {

    disablePoweredBy: function(request, response, next) {
      var expressApp = sails.hooks.http.app;
      expressApp.disable('x-powered-by');
//    response.set('X-Powered-By', 'One Thousand Hamsters');
      next();
    },

    order: [
//    ...
//    'poweredBy',
      'disablePoweredBy',
//    ...
    ]
  }
};

Here, we are retrieving an instance of Express Application from Sails hooks and then using it's disable() method to set the x-powered-by configuration parameter to false value. That will prevent the header from appearing.

And in order to enable this custom middleware, you will need to add it to the order array. You can just replace poweredBy middleware with disablePoweredBy.

Also, by un-commenting the response.set() method you can set your own header value.

Comments