forked from anvaka/corsregistry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (29 loc) · 973 Bytes
/
index.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
// Where do we listen to incoming connections?
var port = process.env.PORT || 3000;
// Whitelist of CORS enabled domains:
var whitelist = [
'http://127.0.0.1:31337',
'http://0.0.0.0:31337',
'http://npmdocs.github.io'
];
// Where to proxy requests:
var endpoint = 'https://registry.npmjs.com';
var express = require('express');
var cors = require('cors');
var proxy = require('express-http-proxy');
var app = express();
app.use('/*', cors(corsOptionsDelegate), createProxyRoute());
app.listen(port, function() {
console.log('Registry proxy started at: http://localhost:' + port);
});
function createProxyRoute() {
return proxy(endpoint, {
forwardPath: function(req) { return req.originalUrl; }
});
}
function corsOptionsDelegate(req, cb) {
var allow = whitelist.indexOf(req.header('Origin')) >= 0;
console.error('Registry access request for %s: %s',
req.header('Origin'), allow ? 'Allow' : 'Deny');
cb(null, { origin: allow });
}