Skip to content

Instantly share code, notes, and snippets.

@gcollazo
Created August 18, 2014 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcollazo/43c4c392f52bc46cc2aa to your computer and use it in GitHub Desktop.
Save gcollazo/43c4c392f52bc46cc2aa to your computer and use it in GitHub Desktop.
Proxy npm with CORS headers. Used in http://jsbin.com/miroq
var http = require('http'),
request = require('request'),
url = require('url');
var port = process.env.PORT || 8000,
registryURL = 'http://registry.npmjs.org:80/';
http.createServer(function (req, res) {
var r = request(url.resolve(registryURL, req.url));
// Add CORS Headers
r.on('response', function(_r) {
_r.headers['Access-Control-Allow-Origin'] = '*';
_r.headers['Access-Control-Allow-Methods'] = '*';
_r.headers['Access-Control-Allow-Headers'] = 'X-Requested-With';
});
// Stream the response
req.pipe(r).pipe(res);
}).listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment