Skip to content

Instantly share code, notes, and snippets.

@iansinnott
Last active April 5, 2019 07:58
Show Gist options
  • Save iansinnott/746f785baecaa0cba9ebd56a56aef844 to your computer and use it in GitHub Desktop.
Save iansinnott/746f785baecaa0cba9ebd56a56aef844 to your computer and use it in GitHub Desktop.
A basic barebones server implementation in node. Zero dependencies.
const http = require('http');
const app = (req, res) => {
req.setEncoding('utf8');
let body = '';
req.on('data', chunk => {
body += chunk;
});
req.on('end', () => {
const data = {
url: req.url,
method: req.method,
headers: req.headers,
body,
};
res.write(JSON.stringify(data));
res.end();
});
req.on('error', err => {
console.error('Oh no...', err.message);
});
};
const server = http.createServer(app);
server.listen(3111, () => {
console.log(`Server listening at ${server.address().port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment