mirror of
https://github.com/nasa/openmct.git
synced 2024-12-19 05:07:52 +00:00
98486f718b
Addresses issue #1158 Passes query parameters on to proxyUrl proxied URL remove the url parameter before passing Naming convention for variables Re-order code to reuse variable
89 lines
2.9 KiB
JavaScript
89 lines
2.9 KiB
JavaScript
/*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(),
|
|
fs = require('fs'),
|
|
request = require('request');
|
|
|
|
// Defaults
|
|
options.port = options.port || options.p || 8080;
|
|
options.directory = options.directory || options.D || '.';
|
|
['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.");
|
|
console.log(" --directory, -D <bundle> Serve files from specified directory.");
|
|
console.log("");
|
|
process.exit(0);
|
|
}
|
|
|
|
app.disable('x-powered-by');
|
|
|
|
// Override bundles.json for HTTP requests
|
|
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));
|
|
});
|
|
|
|
app.use('/proxyUrl', function proxyRequest(req, res, next) {
|
|
var targetUrl = req.query.url;
|
|
var queryParameters = req.query;
|
|
console.log('Proxying request to: ', targetUrl);
|
|
delete queryParameters['url'];
|
|
req.pipe(request({
|
|
url: targetUrl,
|
|
strictSSL: false,
|
|
qs: queryParameters
|
|
}).on('error', next)).pipe(res);
|
|
});
|
|
|
|
// Expose everything else as static files
|
|
app.use(express['static'](options.directory));
|
|
|
|
// Finally, open the HTTP server and log the instance to the console
|
|
app.listen(options.port, function() {
|
|
console.log('Open MCT application running at localhost:' + options.port)
|
|
});
|
|
}());
|