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

When I run the Sails.js application, it automatically adds the following HTTP header for each response: X-Powered-By: "Sails <sailsjs.org>" .

Can I disable or override it?

+8
javascript express
source share
3 answers

Yes, it is quite possible.

You need to disable the Sails middleware called poweredBy , and tell Express.js not to add its own header.

Just update the config/http.js configuration file so that it 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 extract an instance of the Express application from the Sails hooks and then disable() it to set the x-powered-by configuration parameter to false . This will prevent the header from appearing.

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

In addition, without commenting on the response.set() method, you can set your own header value.

+7
source share

Change your config/http.js and set poweredBy to false :

 module.exports.http = { middleware: { poweredBy: false } } 

Since Sails will disable the express X-Powered-By header, there is no need to manually disable it.

+8
source share

No need to create new middleware. You can switch to middleware powered by Sails.js, for example

 module.exports.http = { middleware: { poweredBy: function (req, res, next) { // or uncomment if you want to replace with your own // res.set('X-Powered-By', "Some Great Company"); return next(); } } } 
0
source share

All Articles