Skip to content

Instantly share code, notes, and snippets.

@osher
osher / separated-concerns-frameworkless-http-server.js
Last active Sep 14, 2022
A "frameworkless" http-server - with separation of concerns
View separated-concerns-frameworkless-http-server.js
//code concern
//-------------------------------------------------------------------
const conf = () => { //config
views: { //config
status: 'i\'m alive', //config
greet: 'hello, %s', //config
help: 'supports: /greet or /status', //config
}, //config
web: { port: 3000 }, //config
}; //config
@osher
osher / mixed-concerns-frameworkless-http-server.js
Last active Sep 12, 2022
A "frameworkless" http-server - using naive code (mixed concerns)
View mixed-concerns-frameworkless-http-server.js
//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
View example.for.test.factory.js
/**
Consider the language did not provide us with `.indexOf` and we had to implement it ourselves.
Given that the factory is already implemented, how easy is it to add a new case.
Mind how hoisting helps keep the higher levels above, while implementation details are below.
*/
const Should = require('should');
const SUT = require('./index-of');
View better-example.test.js
/**
This is a better example that does a better job both in testing and in communicating the test-intent.
(with some skipped parts to keep things short)
*/
const helper = require('./helper');
const { user } = require('./fixture');
const SUT = require('../lib/accounts');
const Should = require('should');
describe('lib/accounts - accounts service', () => {
View bad-example.test.js
/**
This is a bad example:
*/
const helper = require('./helper');
const { user } = require('./fixture');
const accounts = require('../lib/accounts');
const Should = require('should');
it('should work', async () => {
await Should(accounts.create(user.admin.registerData).be.rejected();
@osher
osher / parsing-long-string.js
Last active Jan 18, 2016
In favor of loops
View parsing-long-string.js
function buildObject(str) {
var newObj = {};
var len = str.length;
var at = 1;
if (str[0] != "{")
return null;
if (len < 1)
console.log('broke out');