-
Notifications
You must be signed in to change notification settings - Fork 0
/
nocors.js
executable file
·42 lines (34 loc) · 1.04 KB
/
nocors.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
#!/usr/bin/env node
'use strict';
var express = require('express');
var request = require('request');
var morgan = require('morgan')
var args = require('yargs').argv;
var app = express();
const helpText = `
Usage
$ nocors
Options
--port Port you want to run the proxy server
--endpoint API endpoint root URL
Examples
$ nocors --port=1234 --endpoint=http://api.hugethoughts.com
`;
if (args.endpoint === undefined || args.port === undefined || args.help) {
console.log(helpText);
return;
}
app.use(morgan('combined'))
app.use('/', function(req, res) {
var API_END_POINT = args.endpoint + req.url;
var config = {
url: args.endpoint + req.url,
headers: req.headers
}
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, PUT, PATCH, POST, DELETE");
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
req.pipe(request(config)).pipe(res);
});
app.listen(args.port);
console.log('NO_CORS Proxy is running on port ' + args.port);