Skip to content

Instantly share code, notes, and snippets.

@jsynowiec
Last active October 10, 2016 12:02
Show Gist options
  • Save jsynowiec/5a7e65c01f7fa4b96f24fcb44a946e9f to your computer and use it in GitHub Desktop.
Save jsynowiec/5a7e65c01f7fa4b96f24fcb44a946e9f to your computer and use it in GitHub Desktop.
Detect if a module has been run directly from Node.js or imported, read more: https://nodejs.org/api/modules.html#modules_accessing_the_main_module
const dgram = require('dgram');
let server = dgram.createSocket('udp4');
function bind(socket, port = 8088, address = '0.0.0.0', callback = () => {}) {
socket.bind(port, address, callback);
}
// Bind server only if module was run directly, eg. node server.js
if (require.main === module) {
bind(server, process.env.PORT, process.env.HOST);
}
module.exports = {
server,
bind
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment