Skip to content

Instantly share code, notes, and snippets.

@waldher
Created April 24, 2013 16:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waldher/5453355 to your computer and use it in GitHub Desktop.
Save waldher/5453355 to your computer and use it in GitHub Desktop.
NodeJS code for using UNIX sockets at 'tmp/socket' for production environments, and port 3000 for development.
var listenOn = 3000;
if(process.env.NODE_ENV == 'production'){
listenOn = "tmp/socket";
}
var startServer = function(){
app.listen(listenOn, undefined, undefined, function(){
console.log("Listening on " + listenOn);
if(typeof(listenOn) == 'string'){
fs.chmod(listenOn, 0777);
}
});
};
var closeServer = function(){
if(typeof(listenOn) == 'string'){
fs.unlink(listenOn);
}
};
process.on('exit', closeServer);
process.on('SIGINT', function(){
process.exit(0);
});
var fs = require('fs');
if(typeof(listenOn) == 'string'){
fs.exists(listenOn, function(exists){
if(exists){
console.log("WARNING: " + listenOn + " already exists. It will be deleted now.");
fs.unlink(listenOn, function(err){
if(err){
console.log("ERROR: Couldn't delete " + listenOn);
process.exit(1);
return;
}
startServer();
});
} else {
startServer();
}
});
} else {
startServer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment