Using express middleware in Sails.js to serve static files

不问归期 提交于 2019-12-11 01:59:40

问题


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.


回答1:


With the latest Sails (0.12), calling express-style app.use(...) isn't directly possible from config/http.js (or as a policy) because the custom middleware function parameters changed from function (app) to function (req, res, next). This is probably why some of the answers here don't work with Sails 0.12.

This change isn't clearly mentioned in Sails documentation and migration guides, but looking at Express documentation, we see that app.use(...) can accept a single function (req, res, next) argument... so my guess was to assign the parameter to app.use(...) as a custom middleware in config/http.js like so (using connect-history-api-fallback as an example):

// config/http.js
var history = require('connect-history-api-fallback');

module.exports.http = {
  middleware: {
    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'myRequestLogger',
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      '$custom',
      'router',
      'connectHistoryApiFallback', // <==
      'www',
      'favicon',
      '404',
      '500'
    ],
  },

  // Do this instead of app.use(history())
  connectHistoryApiFallback: history()
}

And voilà, it works like a charm!

TL;DR Since Sails 0.12, custom middlewares defined in config/http.js should be of the type function (req, res, next) and not function (app), so instead of calling app.use(someMiddleware), you should place someMiddleware directly in sails.config.http.middleware.order.




回答2:


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();
    }

};



回答3:


On sails 0.12.13 function (app) still works, just pay attention where you place the middleware code (config/http.js):

/**
 * HTTP Server Settings
 * (sails.config.http)
 */

var express = require('express')

module.exports.http = {

  middleware: {

    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      // 'customMiddleware', --> NOT NEEDED
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      '$custom',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ],

  },

  customMiddleware: function (app) {
    // this code will be executed only once during the application startup
    app.use('/public', express.static('/etc'));
  }
};

After doing this you should be able to access the password file in your system using this URL: http://localhost:1337/public/passwd




回答4:


This worked for me in Sails 0.12. Modified the config/http.js and added the following:

/**
 * 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:
 * http://sailsjs.org/#!/documentation/reference/sails.config/sails.config.http.html
 */

var express = require('../node_modules/sails/node_modules/express');

module.exports.http = {

  middleware: {
      // middleware code here ...
  },

  customMiddleware: function(app){
    app.use('/', express.static(process.cwd() + '/assets'));
    return app.use('/bower_components', express.static(process.cwd() + '/bower_components'));
  }
};

Allowed me to do SPP without any engine nor use another http server to serve the static content.




回答5:


EDIT: Sails change since i wrote this answer, please, look if @Meeker answer works.

** OLD ANSWER **

This work for me:

var express = require('express');

module.exports.http = {

  customMiddleware: function (app) {
    app.use('/', express.static(process.cwd() + '/assets/site'));
  },

  middleware: {

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


来源:https://stackoverflow.com/questions/29078276/using-express-middleware-in-sails-js-to-serve-static-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!