-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (72 loc) · 2.63 KB
/
index.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Created by BNB on 4/29/2017.
*/
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var cors = require('cors');
var NoaaData = require('./NoaaData');
var Noaa = new NoaaData();
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// 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 api!' });
});
router.get('/test', function(req, res) {
var coordinatePack = {
east: -119.75490311964109,
north: 39.538635617749755,
south: 39.51195501951582,
west: -119.87841348035886,
};
console.log(JSON.stringify(coordinatePack));
Noaa.resolveParameters(coordinatePack, 2005);
Noaa.GetData(function(result){
res.json(result);
});
});
router.get('/heat', function(req, res){
var coordinates = {
east: parseFloat(req.query.bounds_east),
west: parseFloat(req.query.bounds_west),
north: parseFloat(req.query.bounds_north),
south: parseFloat(req.query.bounds_south)
};
// coordinates = JSON.parse(coordinates);
var year = parseFloat(req.query.year);
Noaa.resolveParameters(coordinates, year);
Noaa.GetData(function(result){
res.json(result);
});
});
router.post('/heatOverTime', function(req, res){
var coordinates = req.body.coordinates;
var startYear = req.body.startYear;
var endYear = req.body.endYear;
startYearIndex = startYear - 2001;
endYearIndex = endYear - 2001;
var yearString = startYearIndex.toString();
if ((endYearIndex - startYearIndex) > 0) {
yearString = require('util').format("%s:%s", startYearIndex, endYearIndex);
}
Noaa.resolveParameters(coordinates, startYear);
Noaa.GetDataSweepYear(yearString, function(result){
res.json(result);
});
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use(router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);
//Here create a REST Api to use.