Skip to content

Instantly share code, notes, and snippets.

@strugee
Created August 14, 2016 04:46
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 strugee/5b4ffe8b8a7d09335ee794713b5b9354 to your computer and use it in GitHub Desktop.
Save strugee/5b4ffe8b8a7d09335ee794713b5b9354 to your computer and use it in GitHub Desktop.
RC interview database
#!/usr/bin/env node
/*
Copyright © 2013-2015 Alex Jordan <alex@strugee.net>
This work is free. Except where otherwise noted, you can redistribute
it and/or modify it under the terms of the Do What The Fuck You Want To
Public License, Version 2, as published by Sam Hocevar.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
// TODO: add tests
// TODO: use npm
// TODO: more ES6 stuff?
// TODO: persistence
// TODO: use proper HTTP status codes on creation
// TODO: logging
// TODO: fix the fact that we've invented a worse form of middleware
'use strict';
var http = require('http');
var url = require('url');
var store = new Map();
console.log('Starting recursecenter-database.js');
console.log('(C) Alex Jordan, licensed under the WTFPL');
function endUnlessGet(req, res) {
if (req.method !== 'GET') {
res.writeHead(405);
res.end('You need to use a GET request');
return true;
}
return false;
}
function ensureQuerystring(req, res) {
// This would be a great place to use lodash, but alas - no external deps
if (Object.keys(req.url.query).length === 0) {
res.writeHead(400);
res.end('You need to specify at least one key-value pair in the query string');
return;
}
}
function handleSet(req, res) {
if (endUnlessGet(req, res)) return;
if (ensureQuerystring(req, res)) return;
Object.assign(store, req.url.query);
res.writeHead(200);
// TODO: better message here
res.end('Created');
}
function handleGet(req, res) {
if (endUnlessGet(req, res)) return;
if (ensureQuerystring(req, res)) return;
var data = new Map();
var key = req.url.query.key;
if (store[key]) {
res.writeHead(200);
res.end(store[key]);
} else {
res.writeHead(404);
res.end(key + ' does not exist in database');
}
}
var server = http.createServer((req, res) => {
req.rawUrl = req.url;
req.url = url.parse(req.rawUrl, true);
switch (req.url.pathname) {
case '/set':
handleSet(req, res);
break;
case '/get':
handleGet(req, res);
break;
default:
res.writeHead(404);
res.end('Cannot ' + req.method + ' ' + req.url.href);
}
});
server.listen(process.env.PORT || 4000, () => {
var address = server.address();
console.log('Listening to port ' + address.port + ' on ' + address.address);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment