Skip to content

Instantly share code, notes, and snippets.

@osher
Last active September 12, 2022 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osher/638c985ba6a5d259ddef76b368b9003f to your computer and use it in GitHub Desktop.
Save osher/638c985ba6a5d259ddef76b368b9003f to your computer and use it in GitHub Desktop.
A "frameworkless" http-server - using naive code (mixed concerns)
//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