Created
April 24, 2013 16:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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