From 3e343bb484c43db0b776c9c0e1fe8ef677479ffe Mon Sep 17 00:00:00 2001 From: Atle Haugan Date: Sun, 13 Mar 2016 21:35:50 +0100 Subject: [PATCH] Refactored + added moment for date formatting --- client.js | 59 ++++++++++++++++++++++++++-------------------------- package.json | 3 ++- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/client.js b/client.js index 9cd3747..929d3c4 100644 --- a/client.js +++ b/client.js @@ -1,46 +1,47 @@ var express = require('express'); var http = require('http'); +var moment = require('moment'); var app = express(); -app.get('/events', function (req, res) { - // var url = 'http://event.polarismedia.no/adressa/search/?dateFrom=2016-02-20&dateTo=2016-03-05&categories=8&page=1'; +var runsBehindProxy = true; + +function getRequestOptionsForPath(path) { var eventHost = 'event.polarismedia.no'; - //var eventPath = '/adressa/events/11948'; - var listEventsPath = '/adressa/search/?dateFrom=2016-03-13&dateTo=2016-03-17&categories=8&page=1'; - var optionsWithProxy = { - host: "www-proxy.statoil.no", - port: 80, - path: listEventsPath, - headers: { - Host: eventHost - } - }; - var optionsWithoutProxy = { - host: eventHost, + var proxyHost = "www-proxy.statoil.no"; + return { + host: runsBehindProxy ? proxyHost : eventHost, port: 80, - path: listEventsPath + path: path, + headers: runsBehindProxy ? {Host: eventHost} : {} }; +} - // Callback function is used to deal with response - var callback = function (response) { +function getListEventsPath() { + // http://event.polarismedia.no/adressa/search/?dateFrom=2016-02-20&dateTo=2016-03-05&categories=8&page=1 + //var eventPath = '/adressa/events/11948'; + var dateFormat = 'YYYY-MM-DD'; + var today = new Date(); + var fiveDaysAhead = new Date(today); + fiveDaysAhead.setDate(fiveDaysAhead.getDate() + 5); + var path = '/adressa/search/?dateFrom=' + moment(today).format(dateFormat) + + '&dateTo=' + moment(fiveDaysAhead).format(dateFormat) + '&categories=8&page=1'; + return getRequestOptionsForPath(path); +} + +app.get('/events', function (requestFromClient, responseToClient) { + requestFromClient = http.request(getListEventsPath(), function (responseFromEventServer) { // Continuously update stream with data var body = ''; - response.on('data', function (data) { + responseFromEventServer.on('data', function (data) { body += data; }); - response.on('end', function () { - // Data received completely. - console.log(body); - var parsedJson = JSON.parse(body); - console.log(parsedJson); - res.send(parsedJson); - //res.send(body); + responseFromEventServer.on('end', function () { + var events = JSON.parse(body); + responseToClient.send(events); }); - }; -// Make a request to the server - req = http.request(optionsWithProxy, callback); - req.end(); + }); + requestFromClient.end(); }); app.listen(3000, function () { diff --git a/package.json b/package.json index f96f5f0..4b9c3ad 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "", "license": "ISC", "dependencies": { - "express": "^4.13.4" + "express": "^4.13.4", + "moment": "2.12.0" } }