description and source-codegulp-server-livereload = function (options) {
var defaults = {
host: 'localhost',
port: 8000,
defaultFile: 'index.html',
fallback: null,
fallbackLogic: function(req, res, fallbackFile) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
fs.createReadStream(fallbackFile).pipe(res);
},
https: false,
open: false,
log: 'info',
clientLog: 'debug',
/**
*
* MIDDLEWARE DEFAULTS
*
* NOTE:
* All middleware should defaults should have the 'enable'
* property if you want to support shorthand syntax like:
*
* webserver({
* livereload: true
* });
*
*/
// Middleware: Livereload
livereload: {
enable: false,
markupHost: null,
port: 35729,
filter: function(filename, cb) {
cb( !(/node_modules/.test(filename)) );
},
clientConsole: false,
},
// Middleware: Directory listing
// For possible options, see:
// https://github.com/expressjs/serve-index
directoryListing: {
enable: false,
path: './',
options: undefined
},
// Middleware: Proxy
// For possible options, see:
// https://github.com/andrewrk/connect-proxy
proxies: []
};
// Deep extend user provided options over the all of the defaults
// Allow shorthand syntax, using the enable property as a flag
var config = enableMiddlewareShorthand(defaults, options, ['directoryListing', 'livereload']);
var logger = bindLogger(config.log, 'server');
var clientLogger = bindLogger(config.clientLog, 'client');
instanceNumber += 1;
var httpsOptions = {
key: fs.readFileSync(config.https.key || __dirname + '/../ssl/dev-key.pem'),
cert: fs.readFileSync(config.https.cert || __dirname + '/../ssl/dev-cert.pem')
};
var openInBrowser = function () {
if (config.open === false) return;
open('http' + (config.https ? 's' : '') + '://' + config.host + ':' + config.port);
openInBrowser = undefined;
};
// connect app
var app = connect();
// Disable browser cache(fix #15)
app.use(function (req, res, next) {
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", 0);
next();
});
// Proxy requests
for (var i = 0, len = config.proxies.length; i < len; i++) {
var proxyoptions = url.parse(config.proxies[i].target);
if (config.proxies[i].hasOwnProperty('options')) {
extend(proxyoptions, config.proxies[i].options);
}
proxyoptions.route = config.proxies[i].source;
app.use(proxy(proxyoptions));
logger.debug(config.proxies[i].source + ' is proxied.');
}
// directory listing
if (config.directoryListing.enable) {
app.use(serveIndex(path.resolve(config.directoryListing.path), config.directoryListing.options));
}
// socket.io
if (config.livereload.enable) {
var snippetParams = [];
if (config.livereload.clientConsole) {
snippetParams.push("extra=capture-console");
}
// If it wasn't provided, use the server host:
var markupHost = !!_.get(config.livereload.markupHost, 'length')
? "'" + config.livereload.markupHost + "'"
: null;
var snippet =
"<script type=\"text/javascript\">"
+ "var _lrscript = document.createElement('script');"
+ "_lrscript.type = 'text/javascript';"
+ "_lrscript.defer = _lrscript.async = true;"
+ "_lrscript.src = '//' + ((" + markupHost + "||location.host).split(':')[0]) + ':"+config.livereload.port+"/livereload.js
?"+snippetParams.join('&')+"';"
+ "document.body.appendChild(_lrscript);"
+ "</script>";
var prepend = function(w, s) {
return s + w;
};
var append = function(w, s) {
return w + s;
}
app.use(inject({
snippet: snippet,
rules: [{
match: /<\/body>/,
fn: prepend
}, {
match: /<\/html>/,
fn: prepend
}, {
match: /<\!DOCTYPE.+>/,
fn: append ...