2015-05-20 16:00:16 +00:00
|
|
|
/*global require,process,console*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Usage:
|
|
|
|
*
|
|
|
|
* npm install minimist express
|
|
|
|
* node app.js [options]
|
|
|
|
*/
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var BUNDLE_FILE = 'bundles.json',
|
|
|
|
options = require('minimist')(process.argv.slice(2)),
|
|
|
|
express = require('express'),
|
|
|
|
app = express(),
|
2016-01-22 20:37:50 +00:00
|
|
|
fs = require('fs'),
|
|
|
|
request = require('request');
|
2015-05-20 16:00:16 +00:00
|
|
|
|
|
|
|
// Defaults
|
|
|
|
options.port = options.port || options.p || 8080;
|
2016-05-06 04:28:41 +00:00
|
|
|
options.directory = options.directory || options.D || '.';
|
2015-05-20 16:00:16 +00:00
|
|
|
['include', 'exclude', 'i', 'x'].forEach(function (opt) {
|
|
|
|
options[opt] = options[opt] || [];
|
|
|
|
// Make sure includes/excludes always end up as arrays
|
|
|
|
options[opt] = Array.isArray(options[opt]) ?
|
|
|
|
options[opt] : [options[opt]];
|
|
|
|
});
|
|
|
|
options.include = options.include.concat(options.i);
|
|
|
|
options.exclude = options.exclude.concat(options.x);
|
|
|
|
|
|
|
|
// Show command line options
|
|
|
|
if (options.help || options.h) {
|
|
|
|
console.log("\nUsage: node app.js [options]\n");
|
|
|
|
console.log("Options:");
|
|
|
|
console.log(" --help, -h Show this message.");
|
|
|
|
console.log(" --port, -p <number> Specify port.");
|
|
|
|
console.log(" --include, -i <bundle> Include the specified bundle.");
|
|
|
|
console.log(" --exclude, -x <bundle> Exclude the specified bundle.");
|
2016-05-06 04:28:41 +00:00
|
|
|
console.log(" --directory, -D <bundle> Serve files from specified directory.");
|
2015-05-20 16:00:16 +00:00
|
|
|
console.log("");
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2016-06-21 17:43:09 +00:00
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
2015-05-20 16:00:16 +00:00
|
|
|
// Override bundles.json for HTTP requests
|
2016-01-08 23:45:30 +00:00
|
|
|
app.use('/' + BUNDLE_FILE, function (req, res) {
|
|
|
|
var bundles;
|
|
|
|
|
|
|
|
try {
|
|
|
|
bundles = JSON.parse(fs.readFileSync(BUNDLE_FILE, 'utf8'));
|
|
|
|
} catch (e) {
|
|
|
|
bundles = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle command line inclusions/exclusions
|
|
|
|
bundles = bundles.concat(options.include);
|
|
|
|
bundles = bundles.filter(function (bundle) {
|
|
|
|
return options.exclude.indexOf(bundle) === -1;
|
|
|
|
});
|
|
|
|
bundles = bundles.filter(function (bundle, index) { // Uniquify
|
|
|
|
return bundles.indexOf(bundle) === index;
|
|
|
|
});
|
|
|
|
|
|
|
|
res.send(JSON.stringify(bundles));
|
|
|
|
});
|
2015-05-20 16:00:16 +00:00
|
|
|
|
2016-01-22 20:37:50 +00:00
|
|
|
app.use('/proxyUrl', function proxyRequest(req, res, next) {
|
|
|
|
console.log('Proxying request to: ', req.query.url);
|
|
|
|
req.pipe(request({
|
|
|
|
url: req.query.url,
|
|
|
|
strictSSL: false
|
|
|
|
}).on('error', next)).pipe(res);
|
|
|
|
});
|
|
|
|
|
2015-05-20 16:00:16 +00:00
|
|
|
// Expose everything else as static files
|
2016-05-06 04:28:41 +00:00
|
|
|
app.use(express['static'](options.directory));
|
2015-05-20 16:00:16 +00:00
|
|
|
|
2016-05-10 19:06:04 +00:00
|
|
|
// Finally, open the HTTP server and log the instance to the console
|
2016-05-10 20:02:19 +00:00
|
|
|
app.listen(options.port, function() {
|
2016-05-10 20:03:07 +00:00
|
|
|
console.log('Open MCT application running at localhost:' + options.port)
|
2016-05-10 20:02:19 +00:00
|
|
|
});
|
2016-05-10 22:14:52 +00:00
|
|
|
}());
|