-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
69 lines (62 loc) · 1.65 KB
/
server.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
var express = require('express');
var app = express();
var db = require('./data-model');
var bodyParser = require('body-parser');
app.use(bodyParser.json({ extended: false }));
// Serve anything under the src directory as a static file
app.use(express.static(__dirname + '/src'));
// A getter for all the paths. They will be listed in reverse order of createdAt for convenience
app.get('/paths', function(request, response) {
db.Path.findAll({
order: [['createdAt', 'DESC']]
})
.then(
function(paths) {
response.json(paths);
},
function(error) {
response.error(error.message);
}
)
});
// A getter for a single path by its ID. This will include the Points of the path
app.get('/paths/:id', function(request, response) {
var id = Number(request.params.id);
console.log(id);
db.Path.findOne({
where: {id: id},
include: [db.Point]
})
.then(
function(path) {
response.json(path);
},
function(error) {
response.error(error.message);
}
);
});
// Catch-all route: if nothing else matches, respond with index.html
app.get('/*', function(request, response) {
response.sendFile(__dirname + '/src/index.html');
});
app.post('/create', function(request, response) {
db.Path.create({
title: request.body.title,
points: request.body.points.map( point => ({
latitude: point.position.lat,
longitude: point.position.lng,
message: point.txt
}))
}, {
include: db.Point
}).then(
function(path) {
response.json(path);
}
);
})
// This line starts the web server
app.listen(process.env.PORT || 8080, function() {
console.log('server started');
});