From: Gustavo Martin Morcuende Date: Wed, 26 Aug 2015 14:57:09 +0000 (+0200) Subject: gulp-my-tasks: using proxy for XHR requests (avoid CORS) X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=2200de2ea56d8940b18c873013ed1cc00f5e81c9;p=JavaScriptForFun gulp-my-tasks: using proxy for XHR requests (avoid CORS) --- diff --git a/gulp/gulp-my-tasks/tasks/server.js b/gulp/gulp-my-tasks/tasks/server.js index a0c3991..7df30be 100644 --- a/gulp/gulp-my-tasks/tasks/server.js +++ b/gulp/gulp-my-tasks/tasks/server.js @@ -2,6 +2,7 @@ var currentDir = process.cwd(); var express = require('express'); +var proxy = require('express-http-proxy'); var app = express(); var favicon = require('serve-favicon'); var logger = require('morgan'); @@ -9,6 +10,10 @@ var port = 9000; var environment = process.env.NODE_ENV; var verbose = process.env.VERBOSE; +// Back-end server for XHR requests: +var serverPort = '8080'; +var serverName = 'localhost'; + app.use(favicon(__dirname + '/favicon.ico')); if (verbose && verbose === true) { app.use(logger('dev')); @@ -20,8 +25,8 @@ switch (environment) { app.use(express.static(currentDir + '/build/')); - // Deep linking - app.use('/*', express.static(currentDir + '/build/index.html')); + // Deep linking (exclude XHR requests) + app.use('/((?!api)).*', express.static(currentDir + '/build/index.html')); break; case 'ngdocs': console.log('ngdocs mode'); @@ -37,11 +42,19 @@ switch (environment) { app.use(express.static(currentDir + '/src/showcase/')); app.use(express.static(currentDir + '/')); - // Deep linking - app.use('/*', express.static(currentDir + '/src/showcase/index.html')); + // Deep linking (exclude XHR requests) + app.use('/((?!api)).*', express.static(currentDir + '/src/showcase/index.html')); break; } +app.use('/', proxy('http://' + serverName, { + forwardPath: function(req, res) { + return require('url').parse(req.url).path; + }, + port: serverPort +})); + + app.listen(port, function() { console.log('Express server listening on port ' + port); console.log('env = ' + app.get('env') +