|
1 | | -let Entry = require('./models/entrymodel'); |
2 | | -let Floor= require('./models/floormodel'); |
3 | | -var express = require('express'); |
| 1 | +const express = require('express'); |
4 | 2 | const path = require('path'); |
5 | | -var app = express(); |
6 | | -const server = '127.0.0.1:27017'; //DB SERVER |
7 | | -const database = 'test'; //DB NAME |
| 3 | +const mongoose = require('mongoose'); |
8 | 4 |
|
| 5 | +const entryRoute = require('./routes/entry'); |
| 6 | +const mapRoute = require('./routes/map'); |
9 | 7 |
|
10 | | - //respond with "hello world" when a GET request is made to the homepage |
11 | | -app.use(express.static(path.join(__dirname, '../build'))); |
12 | | - |
| 8 | +const app = express(); |
| 9 | +const server = '127.0.0.1:27017'; // DB SERVER |
| 10 | +const database = 'test'; // DB NAME |
13 | 11 | const port = process.env.PORT || 5000; |
14 | 12 |
|
15 | | -// console.log that your server is up and running |
16 | | -app.listen(port, () => console.log(`Listening on port ${port}`)); |
| 13 | +// respond with "hello world" when a GET request is made to the homepage |
| 14 | +app.use(express.static(path.join(__dirname, '../build'))); |
17 | 15 |
|
18 | 16 | // create a GET route |
19 | 17 | app.get('/express_backend', (req, res) => { |
20 | 18 | res.send({ express: 'YOUR EXPRESS BACKEND IS CONNECTED TO REACT' }); |
21 | 19 | }); |
22 | 20 |
|
23 | | -var mongoose = require('mongoose'); |
24 | | -var mongoDB = `mongodb://${server}/${database}`; |
| 21 | +const mongoDB = `mongodb://${server}/${database}`; |
25 | 22 | mongoose.connect(mongoDB, { useNewUrlParser: true }); |
26 | | -var db = mongoose.connection; |
| 23 | +const db = mongoose.connection; |
27 | 24 | db.on('error', console.error.bind(console, 'MongoDB connection error:')); |
28 | 25 |
|
29 | | - |
30 | | -var testdata = new Entry({ |
31 | | - coordinate:[{ x:32, y:32, z:23}], |
32 | | - time: new Date(), |
33 | | - temperature: 32.3, |
34 | | - air_velocity: 35.2, |
35 | | -}); |
36 | | -testdata.save(function (err) { |
37 | | - if (err) return handleError(err); |
38 | | - console.log("SAVED"); |
39 | | -}); |
40 | | -app.get("/adddata", (req, res) => { |
41 | | - var testdata = new Entry({ |
42 | | - coordinate:[{ x:32, y:32, z:23}], |
43 | | - time: new Date(), |
44 | | - temperature: 32.3, |
45 | | - air_velocity: 35.2, |
46 | | - }); |
47 | | - testdata.save() |
48 | | - .then(item => { |
49 | | - res.send("Saved to db"); |
50 | | - }) |
51 | | - .catch(err => { |
52 | | - res.status(400).send("unable to save to db"); |
53 | | - }); |
| 26 | +const router = express.Router(); |
| 27 | +app.use('/api', router); |
| 28 | +entryRoute(router); |
| 29 | +mapRoute(router); |
| 30 | +app.listen(port, (req, res) => { |
| 31 | + console.log(`Server is running on ${port}`); |
54 | 32 | }); |
0 commit comments