Skip to content

Instantly share code, notes, and snippets.

@amulyakashyap09
Created September 1, 2018 13:50
Show Gist options
  • Save amulyakashyap09/162bae71a1681557c29d053df04167e6 to your computer and use it in GitHub Desktop.
Save amulyakashyap09/162bae71a1681557c29d053df04167e6 to your computer and use it in GitHub Desktop.
Thought works assignment without using any 3rd party npm modules.
const http = require('http');
const fs = require('fs');
const path = require('path');
const https = require("https");
const querystring = require('querystring');
const td = new Date("2018-09-01")
const config = {
"userId" : "HhjEtD0rD",
"endpoints" : {
"host" : "https://http-hunt.thoughtworks-labs.net",
"challengeUri" : "/challenge",
"inputUri" : "/challenge/input",
"outputUri" : "/challenge/output"
}
};
function requestResponseHandler(request, response){
console.log(`Request came: ${request.url}`);
const contentType = getContentType(request.url);
if(request.url === '/'){
sendFileResponse('index.html', 'text/html', response)
}else if(request.url === '/challenge'){
const url = config.endpoints.host+config.endpoints.challengeUri;
https.get(url, {headers: {
'userId': config.userId
}}, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
sendResponse(body, contentType, response)
});
});
}else if(request.url === '/challenge/input'){
const url = config.endpoints.host+config.endpoints.inputUri;
https.get(url, {headers: {
'userId': config.userId
}}, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
sendResponse(body, contentType, response)
});
});
}else if(request.url === '/challenge/start'){
const inputUrl = config.endpoints.host+config.endpoints.inputUri;
const outputUrl = config.endpoints.host+config.endpoints.outputUri;
https.get(inputUrl, {headers: {
'userId': config.userId,
'Content-Type': contentType
}}, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
let data = JSON.parse(body);
let catMap = {};
let totalValue = 0;
for (var i=0; i<data.length; i++){
var sd = new Date(data[i].startDate)
if (data[i].endDate != null){
var ed = new Date(data[i].endDate)
if (td - sd >= 0 && ed - td >= 0){
if(catMap[data[i].category]){
catMap[data[i].category]++;
}else {
catMap[data[i].category] = 1;
}
totalValue += data[i].price
}
}else if (td - sd >= 0 && data[i].endDate == null) {
if(catMap[data[i].category]){
catMap[data[i].category]++;
}else {
catMap[data[i].category] = 1;
}
totalValue += data[i].price
}
}
let input = JSON.stringify({"totalValue":totalValue});
let req = https.request(outputUrl, {
method: 'POST',
headers: {
'userId': config.userId,
'Content-Type': contentType,
'Content-Length': input.length
}}, res => {
res.setEncoding("utf8");
let body = "";
res.on("data", data => {
body += data;
});
res.on("end", () => {
sendResponse(body, contentType, response)
});
});
req.on('error', (e) => {
console.error(e);
sendResponse(JSON.stringify(e), contentType, response)
});
req.write(input);
req.end();
});
});
}
}
function sendFileResponse(url, contentType, res){
let file = path.join(__dirname, url);
fs.readFile(file, (err, content) => {
if(err){
res.writeHead(404);
res.write(`File '${file}' Not Found!`);
res.end();
console.log("Response: 404 ${file}, err");
}else{
res.writeHead(200, {'Content-Type': contentType});
res.write(content);
res.end();
console.log(`Response: 200 ${file}`);
}
})
}
function sendResponse(content, contentType, res){
res.writeHead(200, {'Content-Type': contentType});
res.write(content);
res.end();
console.log(`Response: 200`);
}
function getContentType(url){
switch (path.extname(url)) {
case '.html':
return 'text/html';
case '.css':
return 'text/css';
case '.js':
return 'text/javascript';
case '.json':
return 'application/json';
default:
return 'application/json';
}
}
const httpServer = http.createServer(requestResponseHandler);
httpServer.listen(8000, () => {
console.log('Node.JS static file server is listening on port 8000')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment