This repository was archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
114 lines (94 loc) · 2.92 KB
/
Copy pathapp.js
File metadata and controls
114 lines (94 loc) · 2.92 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const path = require('path');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const env = process.env.ENV || 'local';
const port = process.env.PORT || 3000;
const host = process.env.HOST || 'localhost';
const doLogging = true;
var app = express();
//------------------------------------------------------------------------------
// Request Pre-processing & middleware
//------------------------------------------------------------------------------
// // Allows parsing of req.body
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// // Sanitise inputs (req.body, req.query and req.params)
// app.use(mongoSanitize());
// // Allow cross-origin HTTP requests
app.use(cors({
origin: '*'
}));
//------------------------------------------------------------------------------
// Express Setup
//------------------------------------------------------------------------------
var router = express.Router();
if (env == 'local') {
app.use(express.static(path.join(__dirname, '/')));
} else {
app.use(express.static(path.join(__dirname, '/build/es6-unbundled')));
}
// Prefix for all routes
app.use('/api', router);
// Start server
console.log("Running in " + env);
console.log("Server starting on " + host + ':' + port);
module.exports = app.listen(port, function (err) {
if (err) {
console.error("Failed to start server!");
console.error(err);
return process.exit(1);
}
});
//------------------------------------------------------------------------------
// Database Kappa
//------------------------------------------------------------------------------
db = [{
name: 'overlay 1',
lat: 50.900202990685784,
lng: -0.9297653110717192,
hdg: 324,
videoURL: 'https://i.ytimg.com/vi/5Nj2BngIko0/maxresdefault.jpg'
},
{
name: 'overlay 2',
lat: 50.925202990685784,
lng: -0.9297653110717192,
hdg: 324,
videoURL: 'https://i.ytimg.com/vi/5Nj2BngIko0/maxresdefault.jpg'
}
]
//------------------------------------------------------------------------------
// Routes
//------------------------------------------------------------------------------
// Do this before all requests
router.use(function (req, res, next) {
log('\nReceived: ' + req.method + ' ' + req.url);
var hasBody = Object.keys(req.body).length;
if (hasBody) {
log(req.body);
}
next();
});
router.get('', (req, res) => {
res.json(db);
});
router.post('', (req, res) => {
db.push(req.body);
res.send('OK');
});
router.delete('', (req, res) => {
delete db[req.query.id];
res.send('OK');
});
// Do this after all requests
router.use(function (req, res, next) {
log('Responding to request with status: ' +
res.statusCode + ' ' + res.statusMessage);
res.end();
});
function log(text) {
if (doLogging) {
console.log(text);
}
}