From 43b13c5e869e1dcc6021a98ef72b90980a5ff3f9 Mon Sep 17 00:00:00 2001 From: Riku Rouvila Date: Tue, 27 Jan 2015 18:54:57 +0200 Subject: [PATCH] add development proxy for API --- README.md | 16 +++++++++++----- gulpfile.js | 23 ++++++++++++++++++++--- package.json | 1 + 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ecf41c8..0b4474b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# KOODIKLINIKKA +# Koodiklinikka ## Getting things up and running - Install [Node.js](http://nodejs.org) @@ -9,7 +9,8 @@ npm install npm start open http://localhost:9001 in your browser -```` +``` + ### Enable LiveReload Install [LiveReload for Chrome](https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei?hl=en) @@ -22,9 +23,14 @@ Install [LiveReload for Chrome](https://chrome.google.com/webstore/detail/livere * Builds everything Minification, uglification and other tasks you're expected to run before deploying your product can be made by running the build command with env variable NODE_ENV set to "production" - - NODE_ENV=production npm run build - +``` +NODE_ENV=production npm run build +``` +## API server +API proxy can be defined with **SERVER** environment variable. +``` +SERVER=http://localhost:9000 npm start +``` ## Development guidelines * **public** - directory should be dedicated only to compiled/copied files from **src** - directory. It should be possible to delete directory completely and after **npm start** or **npm run build** everything should be as they were before the deletion. diff --git a/gulpfile.js b/gulpfile.js index 71c38a5..af6ebd4 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -18,6 +18,7 @@ var browserify = require('browserify'), streamify = require('gulp-streamify'), stylus = require('gulp-stylus'), uglify = require('gulp-uglify'), + httpProxy = require('http-proxy'), watchify = require('watchify'); var production = process.env.NODE_ENV === 'production'; @@ -105,9 +106,25 @@ gulp.task('assets', function() { }); gulp.task('server', function() { - return require('http').createServer(ecstatic({ - root: path.join(__dirname, 'public') - })).listen(9001); + var staticHandler = ecstatic({root: path.join(__dirname, 'public')}); + + var proxy = httpProxy.createProxyServer({ + changeOrigin: true, + target: process.env.SERVER || 'http://koodiklinikka.fi/api' + }); + + proxy.on('error', function(err) { + return console.error(err); + }); + + return require('http').createServer(function(req, res) { + if(req.url.indexOf('/api') > -1) { + req.url = req.url.replace('/api', ''); + proxy.web(req, res); + return; + } + staticHandler.apply(this, arguments); + }).listen(9001); }); gulp.task('watch', function() { diff --git a/package.json b/package.json index 92a6732..52b8a7a 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "gulp-stylus": "1.3.3", "gulp-uglify": "~1.0.1", "gulp-util": "~3.0.1", + "http-proxy": "^1.8.1", "jsx-transform": "^0.10.1", "karma": "~0.12.21", "karma-chrome-launcher": "~0.1.4",