Skip to content

Instantly share code, notes, and snippets.

@aslakhellesoy
Created June 22, 2011 14:50
Show Gist options
  • Save aslakhellesoy/1040253 to your computer and use it in GitHub Desktop.
Save aslakhellesoy/1040253 to your computer and use it in GitHub Desktop.
A dummy npm server useful for publishing private packages
// A very simple npm server that you can `npm publish` to.
// Useful for publishing internal packages.
//
// Writes the published .tgz package to disk under tmp/
//
// In your own project's `package.json`, add this:
//
// "publishConfig":{"registry":"http://localhost:9911"}
//
// Before you publish the first time you also need to
//
// npm adduser
//
// (Just provide some dummy data)
//
// Now that you have a tmp/foo-1.2.3.tgz npm package, you can
// upload it (using scp for example) to a web server, say internalserver.
// Then other projects can depend on your package like so:
//
// "dependencies": {
// "foo": "http://internalserver/foo-1.2.3.tgz"
// }
//
var http = require('http'),
fs = require('fs'),
path = require('path');
http.createServer(function (req, res) {
var buf = null;
req.on('data', function(data) {
if (buf) {
var b = new Buffer(buf.length + data.length);
buf.copy(b, 0, 0, buf.length);
data.copy(b, buf.length, 0, data.length);
buf = b;
} else {
buf = data;
}
});
req.on('end', function() {
if(req.method == 'PUT' && req.headers['content-type'] == 'application/octet-stream') {
var tgz = req.url.split('/').filter(function(name) { return name.match(/\.tgz$/); })[0];
fs.writeFile(path.join('tmp', tgz), buf, function (err) {
if (err) {
res.writeHead(500, {'Content-Type': 'application/json'});
res.end('{}');
} else {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end('{}');
}
});
} else {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end('{}');
}
});
}).listen(9911, "localhost");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment