Skip to content

Instantly share code, notes, and snippets.

@normancapule
Created January 9, 2015 09:16
Show Gist options
  • Save normancapule/e9be149e8067ee3fa9ab to your computer and use it in GitHub Desktop.
Save normancapule/e9be149e8067ee3fa9ab to your computer and use it in GitHub Desktop.
/*jshint unused:false */
/***************
This file allow to configure a proxy system plugged into BrowserSync
in order to redirect backend requests while still serving and watching
files from the web project
IMPORTANT: The proxy is disabled by default.
If you want to enable it, watch at the configuration options and finally
change the `module.exports` at the end of the file
***************/
'use strict';
var httpProxy = require('http-proxy');
var chalk = require('chalk');
/*
* Location of your backend server
*/
var proxyTarget = 'http://localhost:3000/';
var proxy = httpProxy.createProxyServer({
target: proxyTarget,
secure: false,
changeOrigin: false,
xfwd: false
});
proxy.on('error', function(error, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
console.error(chalk.red('[Proxy]'), error);
});
/*
* The proxy middleware is an Express middleware added to BrowserSync to
* handle backend request and proxy them to your backend.
*/
function proxyMiddleware(req, res, next) {
/*
* This test is the switch of each request to determine if the request is
* for a static file to be handled by BrowserSync or a backend request to proxy.
*
* The existing test is a standard check on the files extensions but it may fail
* for your needs. If you can, you could also check on a context in the url which
* may be more reliable but can't be generic.
*/
if (/\.(html|css|js|png|jpg|jpeg|gif|ico|xml|rss|txt|eot|svg|ttf|woff|cur)(\?((r|v|rel|rev)=[\-\.\w]*)?)?$/.test(req.url)) {
next();
} else {
proxy.web(req, res);
}
}
/*
* This is where you activate or not your proxy.
*
* The first line activate if and the second one ignored it
*/
module.exports = [proxyMiddleware];
//module.exports = [];
'use strict';
var gulp = require('gulp');
var util = require('util');
var browserSync = require('browser-sync');
var middleware = require('./proxy');
function browserSyncInit(baseDir, files, browser) {
browser = browser === undefined ? 'default' : browser;
var routes = null;
if(baseDir === 'src' || (util.isArray(baseDir) && baseDir.indexOf('src') !== -1)) {
routes = {
'/bower_components': 'bower_components'
};
}
browserSync.instance = browserSync.init(files, {
startPath: '/',
server: {
baseDir: baseDir,
middleware: middleware,
routes: routes
},
browser: browser,
port: 9000
});
}
gulp.task('serve', ['watch'], function () {
browserSyncInit([
'.tmp',
'src'
], [
'.tmp/{app,components}/**/*.css',
'.tmp/{app,components}/**/*.js',
'src/assets/images/**/*',
'.tmp/*.html',
'.tmp/{app,components}/**/*.html',
'src/*.html',
'src/{app,components}/**/*.html'
]);
});
gulp.task('serve:dist', ['build'], function () {
browserSyncInit('dist');
});
gulp.task('serve:e2e', ['wiredep', 'injector:js', 'injector:css'], function () {
browserSyncInit(['.tmp', 'src'], null, []);
});
gulp.task('serve:e2e-dist', ['build'], function () {
browserSyncInit('dist', null, []);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment