Skip to content

Instantly share code, notes, and snippets.

@dengjonathan
Created September 1, 2016 03:54
Show Gist options
  • Save dengjonathan/729874c88f90af7dd0ef78e2d3615c2b to your computer and use it in GitHub Desktop.
Save dengjonathan/729874c88f90af7dd0ef78e2d3615c2b to your computer and use it in GitHub Desktop.
Simple HTTP server with router for API endpoints
var http = require('http');
var fs = require('fs');
var url = require('url');
var streamMap = require('through2-map');
var port = process.argv[2];
var path = process.argv[3];
var server = http.createServer(function(request, response) {
var get = url.parse(request.url, true);
var endpoint = get.pathname;
var text;
var query = get.query.iso;
var date = new Date(query);
if (endpoint === '/api/parsetime') {
text = JSON.stringify({
hour: date.getHours(),
minute: date.getMinutes(),
second: date.getSeconds()
});
}
if (endpoint === '/api/unixtime') {
text = JSON.stringify({
unixtime: date.getTime()
});
}
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(text);
}).listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment