在Sails.js中使用快递中间件来提供静态文件
我在sails 0.11.0上使用快速中间件时遇到了麻烦。 我在http.js文件中尝试了以下内容。
module.exports.http = { customMiddleware: function (app) { console.log("middleware"); app.use('/test', express.static('****/****/*****/testProject/api/controllers' + '/public/')); } };
但它不起作用,我错过了一些东西。
使用最新的Sails(0.12),因为自定义的中间件function参数从function (app)
改变为了,所以调用express风格的app.use(...)
不能直接从config/http.js
(或者作为策略) function (req, res, next)
。 这可能是为什么这里的一些答案不适用于Sails 0.12。
Sails文档和迁移指南中没有明确提到这种变化,但是看看Express文档 ,我们发现app.use(...)
可以接受单个function (req, res, next)
…所以我猜在config/http.js
像这样使用app.use(...)
作为自定义中间件参数(使用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
定义的自定义中间件应该是typesfunction (req, res, next)
而不是function (app)
,所以不要调用app.use(someMiddleware)
someMiddleware
直接在sails.config.http.middleware.order
。
你的语法有点不对,你需要从提供的callback中返回执行。 它应该看起来就像一个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(); } };
这在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服务器来提供静态内容。
在风帆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')); } };
做完这些之后,你应该可以使用这个URL访问系统中的密码文件: http:// localhost:1337 / public / passwd
编辑:帆改变,因为我写这个答案,请,看,如果@Meeker答案的作品。
**旧答复**
这为我工作:
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' ],