krakig krakig - 28 days ago 42
Node.js Question

Using express middleware in Sails.js to serve static files

I have trouble using the express middleware on sails 0.11.0. I tried the following in http.js file.

module.exports.http = {

customMiddleware: function (app) {
console.log("middleware");
app.use('/test', express.static('****/****/*****/testProject/api/controllers' + '/public/'));
}

};


But it doesn't work, I am missing something.

Answer

Your syntax is a little off and you need to return the execution from the provided call back. It should look just like a controller.action.

I'm including the example from docs, this should help you

/**
 * HTTP Server Settings
 * (sails.config.http)
 *
 * Configuration for the underlying HTTP server in Sails.
 * Only applies to HTTP requests (not WebSockets)
 *
 * For more information on configuration, check out:
 */

module.exports.http = {


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

    myRequestLogger: function (req, res, next) {
        console.log("Requested :: ", req.method, req.url);
        return next();
    }

};