Last active
April 5, 2019 07:58
-
-
Save iansinnott/746f785baecaa0cba9ebd56a56aef844 to your computer and use it in GitHub Desktop.
A basic barebones server implementation in node. Zero dependencies.
This file contains 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
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