Last active
September 1, 2016 03:53
-
-
Save dengjonathan/97f5554faccc87d82199ce30a7adb67f to your computer and use it in GitHub Desktop.
Create a basic HTTP server with node that returns the same file from a read stream on every request
This file contains 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 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