var http = require('http');
var dataSource = require('./datasource.js');
dataSource.parseAllData();

var gju = require('geojson-utils');
var express = require('express');
var app = express();

app.listen(3000);

app.get('/*',function(req,res,next){
  res.header('Access-Control-Allow-Origin' , "*" );
  next();
});

function validElementsFromCenter(pointsArray, polygon, extension) {
  var validData = [];

  for (var i in pointsArray) {
    var feature = pointsArray[i];
    var point = feature['geometry'];

    if (gju.pointInPolygon(point, polygon) && extension(feature)) {
      validData.push(feature);
    }
  }

  return validData;
}

app.get('/elements', function(request, response) {
  var polygon = {
    "type": "Polygon",
    "coordinates": [[[request.query.min_lng, request.query.min_lat],
    [request.query.max_lng, request.query.min_lat],
    [request.query.max_lng, request.query.max_lat],
    [request.query.min_lng, request.query.max_lat],
    [request.query.min_lng, request.query.min_lat]]]
  };

  var json = {};

  var sources = dataSource.sources();
  for (var key in sources) {
    if (sources.hasOwnProperty(key)) {
      var source = sources[key];
      dataSource.getDataForKey(key, function (data) {
        var pointsArray = data['features'];
        json[key] = validElementsFromCenter(pointsArray, polygon, source.responseExtension);
      });
    }
  }

  response.json(json);
});

app.get('/vdq', function(req, res) {
  var options = {
    host: 'acc-api.ville.quebec.qc.ca',
    port: 80,
    path: '/stationnement/rest/vdqpark/availabilityservice?response=json'
  };

  var response = "";
  http.get(options, function(resp){
    resp.on('data', function(chunk){
      response = response + chunk;
    });
    resp.on('end', function() {
      res.setHeader('Content-Type', 'application/json');
      res.send(response);
    });
  }).on("error", function(e){
    console.log("Got error: " + e.message);
    res.json({status:"Error"});
  });
});

app.get('/update', function(req, res){
  var download = require('./download.js');
  download.updateData();

  res.json({status: "ok"});
});

app.get(/^((?!(elements|vdq|update))(.+)$)/, function(req, res) {
  res.sendfile('public/' + req.params[0]);
});


//var download = require('./download.js');
//download.updateData();
//setInterval(function(){
 // download.updateData();
//},86400000);

@@ -1404,25 +1404,24 @@
 (json);%0A%7D);%0A
-%0A
 app.get('/vd
@@ -2011,16 +2011,17 @@
 eq, res)
+ 
 %7B%0A  var 
@@ -2124,114 +2124,49 @@
 app.
-get(/%5E((?!(elements%7Cvdq%7Cupdate))(.+)$)/, function(req, res) %7B%0A  res.sendfile('public/' + req.params%5B0%5D);%0A%7D
+use(express.static(__dirname + '/public')
 );%0A%0A

Use express.static to serve /public dir