Skip to content

Commit

Permalink
Refactored + added moment for date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Atle Haugan committed Mar 13, 2016
1 parent 6da1405 commit 3e343bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
59 changes: 30 additions & 29 deletions client.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.13.4"
"express": "^4.13.4",
"moment": "2.12.0"
}
}

0 comments on commit 3e343bb

Please sign in to comment.