在Sails.js中使用express中间件来提供静态文件

时间:2015-03-16 13:39:56

标签: node.js static sails.js middleware

我在帆0.11.0上使用快速中间件时遇到问题。我在http.js文件中尝试了以下内容。

module.exports.http = {

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

};

但它不起作用,我错过了一些东西。

5 个答案:

答案 0 :(得分:3)

使用最新的Sails(0.12),从app.use(...)(或作为策略)无法直接调用express-style config/http.js,因为自定义中间件函数参数已从function (app)更改到function (req, res, next)。这可能是为什么这里的一些答案不适用于Sails 0.12。

在Sails文档和迁移指南中未明确提及此更改,但查看Express documentation,我们发现app.use(...)可以接受单个function (req, res, next)参数...所以我的猜测是将app.use(...)中的参数作为自定义中间件分配给config/http.js,如下所示(以connect-history-api-fallback为例):

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

瞧,它就像一个魅力!

TL; DR 由于Sails 0.12,config/http.js中定义的自定义中间件应该属于function (req, res, next)而不是function (app),因此不应调用{{ 1}},您应将app.use(someMiddleware)直接放在someMiddleware

答案 1 :(得分:2)

您的语法稍微偏离,您需要从提供的回调中返回执行。它看起来应该像controller.action。

我包含来自文档的示例,这应该可以帮助您

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

};

答案 2 :(得分:1)

在帆上0.12.13 function (app)仍然有效,只需注意放置中间件代码的位置(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'));
  }
};

执行此操作后,您应该可以使用以下网址访问系统中的密码文件:http://localhost:1337/public/passwd

答案 3 :(得分:0)

这在Sails 0.12中对我有用。修改了 config / http.js 并添加了以下内容:

/**
 * 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'));
  }
};

允许我在没有任何引擎的情况下执行SPP,也不使用其他http服务器来提供静态内容。

答案 4 :(得分:-2)

编辑:自从我写完这个答案后,请改变,请查看@Meeker的答案是否有效。

** OLD ANSWER **

这项工作对我来说:

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'
    ],