Skip to content

Instantly share code, notes, and snippets.

@vadym-vorobel
Last active April 23, 2018 07:57
Show Gist options
  • Save vadym-vorobel/99ca0ee6fb9c4aad0fce666769b95a8b to your computer and use it in GitHub Desktop.
Save vadym-vorobel/99ca0ee6fb9c4aad0fce666769b95a8b to your computer and use it in GitHub Desktop.
Working with async functions and modules
Some important data
module.exports = {
add: (a, b) => a + b,
sub: (a, b) => a - b,
mul: (a, b) => a * b,
div: (a, b) => a / b,
};
const fs = require('fs');
const http = require('http');
const { add } = require('./arithmetic');
const PORT = process.env.PORT || 3000;
const [_, __, a = 0, b = 0] = process.argv;
console.log(`Adding ${a} + ${b} = ${add(a, b)}`);
http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
fs.readFile('data.txt', (error, data) => {
if (!error) {
res.end(data.toString());
} else {
res.end('An error ocurred!', error.toString());
}
});
}).listen(PORT, () => {
console.log(`Listening port ${PORT}...`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment