Skip to content
Permalink
Browse files

feat(option): add httpProxy option, useful when you want to test exte…

…rnal api(s)
  • Loading branch information
jyounce committed Aug 11, 2015
1 parent eb695aa commit b30b2ae64ed75883c51969b4e9a4632e1cf03e2a
@@ -146,6 +146,7 @@ gulp rapid-build:test:prod
# exclude.from.spaFile[scripts|styles] = (array of strings) = file paths: exclude script or style files from automatically being generated in the spa.html file
# test.browsers = (array of browser names) = phantomjs will run by default, optional browser names are ['chrome', 'firefox', 'ie', 'safari']
# server.node_modules = (array of module names) = node_modules you would like to copy to the server dist, example: ['q']
# httpProxy = (array of objects) = object format: { context: array or string, options: object } for details see: https://www.npmjs.com/package/http-proxy-middleware
# =============================================================================================================================================================================================================================
```

@@ -23,6 +23,7 @@ module.exports = (rbDir, options) ->
config = require("#{config.req.config}/config-exclude") config, options
config = require("#{config.req.config}/config-templates") config
config = require("#{config.req.config}/config-temp") config
config = require("#{config.req.config}/config-http-proxy") config, options
config = require("#{config.req.config}/config-node_modules") config, options
config = require("#{config.req.config}/config-order") config, options
config = require("#{config.req.config}/config-globs") config
@@ -0,0 +1,25 @@
module.exports = (config, options) ->
log = require "#{config.req.helpers}/log"
test = require("#{config.req.helpers}/test")()

# init httpProxy
# ==============
httpProxy = options.httpProxy or [] # format: { context: [] or '', options: {} }

# add httpProxy to config
# =======================
config.httpProxy = httpProxy

# logs
# ====
# log.json httpProxy, 'httpProxy ='

# tests
# =====
test.log 'true', config.httpProxy, 'add httpProxy to config'

# return
# ======
config


@@ -8,6 +8,10 @@ module.exports = (config, options) ->
rbModules = ['express']
appModules = options.server.node_modules or []

# http proxy
# ==========
rbModules.push 'http-proxy-middleware' if config.httpProxy.length

# init node_modules
# =================
node_modules =
@@ -100,6 +100,9 @@ module.exports = (config, options) ->
options.server = {} if not isType.object options.server
options.server.node_modules = null if not isType.array options.server.node_modules

proxyOptions = ->
options.httpProxy = null if not isType.array options.httpProxy

# init
# ====
distAndSrcOptions() # must be first
@@ -112,6 +115,7 @@ module.exports = (config, options) ->
testOptions()
serverDistOptions()
serverOptions()
proxyOptions()

# logs
# ====
@@ -56,6 +56,7 @@
"gulp-uglify": "~1.2.0",
"gulp-util": "~3.0.6",
"gulp-watch": "~4.3.4",
"http-proxy-middleware": "~0.5.0",
"jasmine-core": "~2.3.4",
"karma": "~0.13.8",
"karma-jasmine": "~0.3.6",
@@ -0,0 +1,14 @@
module.exports = (app, config, opts) ->
return if not config.httpProxy.length
proxyMidware = require 'http-proxy-middleware' # express middleware
proxies = []

for proxy in config.httpProxy
continue if not proxy.context
proxies.push proxyMidware proxy.context, proxy.options

return if not proxies.length

# add middleware to express app
# =============================
app.use proxies
@@ -8,6 +8,7 @@ spa = config.spa.dist.file # ex: spa.html
clientDirPath = path.resolve dir, '..', '..', config.dist.app.client.dirName # creates absolute path to the client folder
appFilePath = path.resolve dir, '..', config.dist.app.server.scripts.file
serverDirPath = path.resolve dir, '..', '..', config.dist.app.server.dirName
proxyFilePath = path.join dir, 'http-proxy.js'

app.use express.static clientDirPath
app.listen port, ->
@@ -23,6 +24,10 @@ opts =
client: clientDirPath
server: serverDirPath

# load optional http proxy
# ========================
require(proxyFilePath) app, config, opts if config.httpProxy.length

# load optional app server dist entry script
# ==========================================
try require(appFilePath) app, opts

0 comments on commit b30b2ae

Please sign in to comment.
You can’t perform that action at this time.