Created
October 7, 2016 07:10
-
-
Save v3rse/8af2b72c887cf2e1e89422cf1be5b6b2 to your computer and use it in GitHub Desktop.
A simple in-memory database
This file contains hidden or 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
var http = require('http'); | |
var url = require('url'); | |
var data = {}; | |
http.createServer(function(req, res){ | |
//use querystring module on query strings and stores in 'query' object | |
var parseUrl = url.parse(req.url, true, true); | |
var statusCode = 404; | |
var content = "404 - Not Found" | |
if(parseUrl.pathname === "/set"){ | |
console.log("Inserting value into database"); | |
statusCode = 200; | |
//assign new query object's contents. don't forget previous state of 'data' | |
Object.assign(data,parseUrl.query,data); | |
content = "Data inserted"; | |
}else if(parseUrl.pathname === "/get"){ | |
console.log("Retrieving value from database"); | |
statusCode = 200; | |
//get search key | |
searchKey = parseUrl.query.key; | |
content = data[searchKey]; | |
} | |
res.writeHead(statusCode,{"Content-Type":"text/plain"}); | |
res.end(content); | |
}).listen(4000); | |
console.log("Started DB server..."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment