SailsJS: Rewrite non-www URL to www

How can you rewrite a url so whenever some visits let's say:

example.com

it redirects permanently to:

www.example.com

Do this in config/http.js(the custom bit is 'redirectToWWW')

module.exports.http = {

  middleware: {

    order: [
       'redirectToWWW',
       'startRequestTimer',
       'cookieParser',
       'session',
       'myRequestLogger',
       'bodyParser',
       'handleBodyParserError',
       'compress',
       'methodOverride',
       'poweredBy',
       '$custom',
       'router',
       'www',
       'favicon',
       '404',
       '500'
    ],

  redirectToWWW: function(req, res, next) {
        var host = req.header("host");
        if (host.match(/^www\..*/i)) {
          next();
        } else {
          res.redirect(301, "http://www." + host);
        }
  },

  }
};