Created
September 22, 2014 02:13
-
-
Save jgatjens/40cd97dba9bba29e74a1 to your computer and use it in GitHub Desktop.
server.js
This file contains hidden or 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
'use strict'; | |
var gulp = require('gulp'); | |
var browserSync = require('browser-sync'); | |
var httpProxy = require('http-proxy'); | |
/* This configuration allow you to configure browser sync to proxy your backend */ | |
var proxyTarget = 'http://server/context/'; // The location of your backend | |
var proxyApiPrefix = 'api'; // The element in the URL which differentiate between API request and static file request | |
var proxy = httpProxy.createProxyServer({ | |
target: proxyTarget | |
}); | |
function proxyMiddleware(req, res, next) { | |
if (req.url.indexOf(proxyApiPrefix) !== -1) { | |
proxy.web(req, res); | |
} else { | |
next(); | |
} | |
} | |
function browserSyncInit(baseDir, files, browser) { | |
browser = browser === undefined ? 'default' : browser; | |
browserSync.instance = browserSync.init(files, { | |
startPath: '/', | |
server: { | |
baseDir: baseDir, | |
middleware: proxyMiddleware | |
}, | |
browser: browser, | |
minify: false, | |
logFileChanges: false | |
}); | |
} | |
gulp.task('serve', ['watch'], function () { | |
browserSyncInit([ | |
'app', | |
'.tmp' | |
], [ | |
'app/*.html', | |
'.tmp/styles/**/*.css', | |
'app/scripts/**/*.js', | |
'app/common/**/{*.js,*.html,*.scss}', | |
'app/pages/**/{*.js,*.html}' | |
// 'app/images/**/*' | |
]); | |
}); | |
gulp.task('serve:dist', ['build'], function () { | |
browserSyncInit('dist'); | |
}); | |
gulp.task('serve:e2e', function () { | |
browserSyncInit(['app', '.tmp'], null, []); | |
}); | |
gulp.task('serve:e2e-dist', ['watch'], function () { | |
browserSyncInit('dist', null, []); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment