我使用grunt-contrib-connect作为文件服务器。现在,我希望仅当/some/ file /path/with/filename-dbg.js不存在时,才使用/filename-dbg.js将文件请求从/some/file/path/with/filename-dbg.js重写到/some/file/path/with/filename.js。
我的第一个想法是使用grunt connect-rewrite,但它不支持apache modrewrite模块中使用的-f或-d标志。
第二个想法是在grunt-contrib-connect的中间件配置中添加一个功能。
有没有什么办法来实现它呢?
致以最好的问候,康斯坦丁
发布于 2014-03-29 04:54:55
你可以通过grunt connect-rewrite来达到这个目标,只需要把它作为最后一个中间件。但我建议使用https://www.npmjs.org/package/http-rewrite-middleware而不是更灵活的解决方案……
也就是说,最终的解决方案可能是这样的:
var rewriteModule = require('http-rewrite-middleware');
//...
grunt.initConfig({
connect: {
options: {
port: 9000,
hostname: 'localhost'
},
development: {
options: {
middleware: function (connect, options) {
var middlewares = [];
if (!Array.isArray(options.base)) {
options.base = [options.base];
}
var directory = options.directory || options.base[options.base.length - 1];
options.base.forEach(function (base) {
// Serve static files.
middlewares.push(connect.static(base));
});
// Make directory browse-able.
middlewares.push(connect.directory(directory));
// ... everything else here
// RewriteRules support
middlewares.push(rewriteModule.getMiddleware([
{from: '^(.*)-dbg.js', to: '$1.js'}
]));
return middlewares;
}
}
}
}
});https://stackoverflow.com/questions/22107475
复制相似问题