forked from nikmd23/ballpark-tracker
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
61 lines (51 loc) · 1.62 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
var io = require("socket.io");
var http = require("http");
var fs = require('fs')
var path = require('path');
var express = require('express');
var app = express();
var mongoClient = require('mongodb').MongoClient;
var mongoUrl = 'mongodb://mongo:27017/';
var mongoDb = 'ballparkTracker';
var mongoCollection = 'parks';
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// app.use(express.json());
//main app page
app.get('/', (req, res) => res.render('index', { AZURE_MAPS_KEY: process.env.AZURE_MAPS_KEY }));
// return a json list of ballparks
app.get('/api/parks', (req, res) => {
mongoClient.connect(mongoUrl, (_err, db) => {
var dbo = db.db(mongoDb);
var query = { 'properties.League': 'MLB' };
dbo.collection(mongoCollection).find(query).toArray(function (_err, result) {
res.json(result);
db.close();
});
});
});
//update the visited bit in the db
app.put('/api/update', (req, res) => {
var body = req.body;
if (!body) {
res.status(500).send({
message: 'PUT request missing required body.'
});
} else {
mongoClient.connect(mongoUrl, (_err, db) => {
var dbo = db.db(mongoDb);
var query = { 'properties.VenueId': parseInt(body.id) };
var patch = { $set: { 'properties.Visited': body.visited } };
dbo.collection(mongoCollection).updateOne(query, patch, (_err, result) => {
res.sendStatus(200);
db.close();
});
});
}
});
//configuring static assets (css/js)
app.use(express.static('public'))
var server = http.createServer(app);
io(server);
server.listen(3000);
console.log('Running at http://localhost:3000');