-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (47 loc) · 1.37 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
53
54
55
56
57
58
59
60
61
var express = require('express');
var request = require('request');
// Create the app
var app = express();
app.use(express.methodOverride());
// CORS middleware
//
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain);
// Routes
app.get('/', function(req, res) {
res.set('Content-Type', 'application/json');
try {
request({
url: req.query.url,
headers: {
accept: req.headers.accept,
authorization: req.headers.authorization,
'Content-Type': 'application/json'
}
}).pipe(res);
} catch (err) {
res.send(
'Error: please specify a URL (e.g. ?url=http://example.com/path)');
}
console.log('GET /', req.query);
});
var args = process.argv.splice(2);
var port = parseInt(args[0]);
if (isNaN(port)) {
port = 3000;
}
// Start the server
app.listen(port);
console.log('Started app. Listening on port', port);