Skip to content

Instantly share code, notes, and snippets.

@celestelayne
Last active December 18, 2016 03:40
Show Gist options
  • Save celestelayne/01bf028851381819904bb7ccd37e0731 to your computer and use it in GitHub Desktop.
Save celestelayne/01bf028851381819904bb7ccd37e0731 to your computer and use it in GitHub Desktop.
Simple database server
/*
Before your interview, write a program that runs a server that is accessible on http://localhost:4000/. When your server receives a request on http://localhost:4000/set?somekey=somevalue it should store the passed key and value in memory. When it receives a request on http://localhost:4000/get?key=somekey it should return the value stored at somekey.
During your interview, you will pair on saving the data to a file. You can start with simply appending each write to the file, and work on making it more efficient if you have time.
http://localhost:4000/set?name=harold
http://localhost:4000/get?key=name
*/
var http = require('http');
var url = require('url');
var fs = require('fs');
var file = './kv_store.json';
var data = require(file);
// create object and save key value pairs
var db = {
setKeyVal: function(key, val){
this[key] = val;
},
getVal: function(key){
return this[key];
}
}
http.createServer(function(req, res){
// parse the url so it's returned as an object
// and allows access to its data
var objUrl = url.parse(req.url, true);
// controllers
if(objUrl.pathname === '/set' && req.method == 'GET'){
// SET route
// loop through objects' keys and load into memory
Object.keys(objUrl.query).forEach(function(key){
var value = objUrl.query[key];
db.setKeyVal(key, value);
});
// take the query straight from the url
var content = JSON.stringify(objUrl.query);
// create a file and pass the database content into it
fs.writeFile(file, JSON.stringify(db), null, function(err){
console.log(db)
});
// append new content to file ... synchronously so as not to get multiple objects in file
fs.appendFileSync(file, content, null);
console.log(content);
var resObj = {
success : 'Updated Successfully'
}
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(resObj));
} else if (objUrl.pathname === '/get' && req.method == 'GET') {
// GET route
var k, v;
Object.keys(data).forEach(function(key){
// console.log('line 65: ', k)
k = JSON.stringify(data[key]);
});
console.log('line 68: ', k)
// if not the key, return an error
if(!k){
var resObj = {
status : 'error',
error : 'Invalid key'
}
res.writeHead(200, {'Content-Type':'application/json'});
res.end(JSON.stringify(resObj));
}
// if the value is not a string, return an error
Object.keys(data).forEach(function(value){
v = data[value];
});
console.log('line 85: ', v);
if(typeof v !== 'string'){
var resObj = {
status : 'error',
error : 'Key not found',
}
res.writeHead(200, {'Content-Type':'application/json'});
res.end(JSON.stringify(resObj));
}
// if there is a successful request
var resObj = {
status : 'success',
data : v,
}
res.writeHead(200, {'Content-Type':'application/json'});
res.end(JSON.stringify(resObj));
} else {
// route not defined
var resObj = {
"status": 404,
"error": "404 Not Found"
}
res.writeHead(404, {'Content-Type':'application/json'});
res.end(JSON.stringify(resObj));
}
}).listen(4000);
console.log('Magic is happening on port 4000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment