-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
73 lines (63 loc) · 2.39 KB
/
server.js
File metadata and controls
73 lines (63 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var async = require('async'); // http://www.sebastianseilund.com/nodejs-async-in-practice
var weather = require('./weather.js');
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next(); // make sure we go to the next routes and don't stop here
});
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our cycle api!' });
});
router.get('/v1/cycle', function(req, res, next) {
var location = req.query.location;
var airQualities = req.query.airquality;
var shouldicycle = {};
shouldicycle.pollution = [];
async.parallel([
// Get weather
function(callback) {
if (location) {
weather.lookupCurrentWeather(location, shouldicycle, callback);
}
else {
callback();
}
},
// Get airquality
function(callback) {
if (airQualities) {
var airQualityArray = airQualities.split(',');
async.forEach(airQualityArray, function(airQuality, airQualityCallback) {
weather.lookupAirQuality(airQuality, shouldicycle, airQualityCallback);
}, callback);
}
else {
callback();
}
}
],
// This is called when weather and airquality are done
function(err) {
if (err) return next(res.status(err.code).json(err));
res.json(shouldicycle);
});
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);