这个问题很容易解释。我目前有两个版本的连接任务,一个用于grunt-contrib-proxy,另一个用于modRewrite。但我需要同时使用它们。
这样,如果加载了某些选择的'routes':['/ login','/ ord']我需要代理请求,但如果加载了其他任何内容,我需要重定向到/ index html的。这样做的原因是我使用的是backbone.js,所以localhost:8000 / fun应该在骨干中加载有趣的路径。
以下所有连接任务都可以完美地工作(单独使用)。
TL; DR 如何组合这些连接任务:
connect: {
server: {
options: {
port: 9001,
middleware: function(connect, options) {
var middlewares, proxy;
proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
middlewares = [proxy, connect["static"](options.base), connect.directory(options.base)];
return middlewares;
}
},
proxies: [
{
context: ['/login', '/ord'],
host: '10.10.1.13',
https: false
}
]
}
}
connect: {
server: {
options: {
port: 9001,
open: true,
base: ['./'],
middleware: function(connect, options) {
var middlewares;
middlewares = [];
middlewares.push(modRewrite(['^[^\\.]*$ /index.html [L]']));
options.base.forEach(function(base) {
return middlewares.push(connect["static"](base));
});
return middlewares;
}
}
}
}
如果您愿意,他们会在咖啡馆里:
connect:
server:
options:
port: 9001
# keepalive: true
middleware: (connect, options) ->
proxy = require('grunt-connect-proxy/lib/utils').proxyRequest
middlewares = [
proxy
connect.static(options.base)
connect.directory(options.base)
]
middlewares
proxies: [
{
context: ['/login', '/ord']
host: '10.10.1.13'
https: false
}
]
connect:
server:
options:
port: 9001
open: true
base: ['./']
middleware: (connect, options) -> (
middlewares = []
middlewares.push(modRewrite(['^[^\\.]*$ /index.html [L]']))
options.base.forEach( (base) ->
middlewares.push(connect.static(base))
)
middlewares
)
非常感谢。
答案 0 :(得分:2)
This article was helpful in solving my issues.
咖啡溶液:
connect:
server:
options:
port: 9001
base: './'
# keepalive: true
uselessMiddleware: (req, res, next) ->
next()
middleware: (connect, options) ->
# same as in grunt-contrib-connect
directory = options.directory || options.base[options.base.length - 1]
if (!Array.isArray(options.base))
options.base = [options.base]
# custom middleware
proxy = require('grunt-connect-proxy/lib/utils').proxyRequest
middlewares = [
# proxy
proxy
connect.static(options.base[0])
connect.directory(options.base[0])
# mod rewrite
modRewrite(['^[^\\.]*$ /index.html [L]'])
connect.static(options.base[0])
]
# same as in grunt-contrib-connect
options.base.forEach( (base) ->
middlewares.push(connect.static(base))
)
middlewares.push(connect.directory(directory))
middlewares
proxies: [
{
context: ['/login', '/UserData', '/logout']
host: '10.10.1.13'
https: false
}
]