-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
53 lines (47 loc) · 1.51 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//jshint node: true
var connect = require('connect'),
http = require('http'),
httpProxy = require('http-proxy'),
cors = require('cors'),
bodyParser = require('body-parser'),
path = require('path'),
serveStatic = require('serve-static'),
fs = require('fs'),
app, port, host, server;
process.on('uncaughtException', function (err) {
"use strict";
console.error("Uncaught Exception");
console.error(err);
console.error(err.stack);
});
app = connect();
app.use(bodyParser.json());
port = process.env.PORT || 9252;
host = process.env.HOST || "127.0.0.1";
// Override default app configuration
app.use('/js/default.js', function (req, res) {
"use strict";
res.setHeader('Content-Type', 'text/javascript');
fs.createReadStream(path.join(__dirname, '/public/public.js')).pipe(res);
});
// CORS proxy
(function () {
"use strict";
var proxy, proxyRequest;
proxy = httpProxy.createProxyServer();
proxy.on('proxyReq', function (proxyReq, req, res, options) {
proxyReq.path = 'http://' + req.url.substr(1);
});
proxyRequest = function (req, res) {
proxy.web(req, res, { target: 'http://' + req.url.substr(1) });
};
app.use('/proxy/', cors());
app.use('/proxy/', proxyRequest);
}());
// Serve static content
app.use(serveStatic(path.join(__dirname, '/node_modules/alir')));
// Starts the server itself
server = http.createServer(app).listen(port, host, function () {
"use strict";
console.log("Server listening to %s:%d", host, port);
});