Created
March 31, 2015 01:00
-
-
Save yargalot/3bd3a621b90316f15708 to your computer and use it in GitHub Desktop.
Grunt-contrib-connect working with proxy-middleware cause grunt-connect-proxy is shit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = require('url'); | |
var proxy = require('proxy-middleware'); | |
module.exports.tasks = { | |
connect: { | |
options: { | |
port: 9001, | |
hostname: 'localhost', | |
base: '<%= location.frontEnd %>/dist', | |
livereload: true | |
}, | |
server: { | |
options: { | |
open: 'http://localhost:9001', | |
proxy: 'http://localhost:9080/web/tasks/', | |
middleware: function(connect, options) { | |
var middlewares = []; | |
var proxyUrl = options.proxy || 'http://localhost:8000'; | |
var proxyOptions = url.parse(proxyUrl); | |
proxyOptions.route = '/api'; | |
middlewares.push(proxy(proxyOptions)); | |
middlewares.unshift(function(req, res, next) { | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); | |
res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); | |
next(); | |
}); | |
if (!Array.isArray(options.base)) { | |
options.base = [options.base]; | |
} | |
//Options for connect.static module. See http://www.senchalabs.org/connect/static.html | |
var defaultStaticOptions = {}; | |
var directory = options.directory || options.base[options.base.length - 1]; | |
options.base.forEach(function(base) { | |
// Serve static files. | |
var path = base.path || base; | |
var staticOptions = base.options || defaultStaticOptions; | |
middlewares.push(connect.static(path, staticOptions)); | |
}); | |
// Make directory browse-able. | |
middlewares.push(connect.directory(directory.path || directory)); | |
return middlewares; | |
}, | |
} | |
}, | |
test: {} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment