William Shallum

node.js: Set unix socket permissions

Posted Aug 25 2010, 23:18 by William Shallum [updated Jul 5 2015, 08:23]

Unix socket permissions depend on umask, so set umask before listen and reset it after listen succeeds (the callback parameter). The following code will do that.

var http = require("http");
var oldUmask = process.umask(0000);
var server = http.createServer(function(request, response) { 
  // ...
});

server.listen("/some/socket/path", function() {
  process.umask(oldUmask);
});

If there is worry about what happens between the listen() succeeding and the callback being called, you can always set the permissions explicitly (not depending on umask) with a fs.chmodSync call when the listen succeeds.