Last active
September 12, 2022 16:13
-
-
Save osher/638c985ba6a5d259ddef76b368b9003f to your computer and use it in GitHub Desktop.
A "frameworkless" http-server - using naive code (mixed concerns)
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
//code concerns | |
//------------------------------------------------------------------- | |
console.log('starting greeter'); //binary+logging | |
require('http').createServer((req, res) => { //binary+app | |
const [action] = req.url.slice(1).match(/[^\/]+/); //route | |
switch(true) { //route | |
case action == 'status': //webCtrl | |
return res.end('i\'m alive'); //view+webCtrl | |
case action == 'greet': //webCtrl | |
const name = url.slice(7) || 'stranger'; //webCtrl | |
console.log('greeting', { name }) //logging | |
return res.end(`hello, ${name}`); //view+webCtrl | |
default: //route | |
console.log('no such url', { url: req.url }); //logging | |
res.statusCode = 404; //webCtrl | |
res.end('supports: /greet or /status'); //view+webCtrl | |
} //route | |
}).listen( //binary+app | |
3000, //binary | |
err => console.log(err || "Greeter on port 3000"), //binary+logging | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment