|
| 1 | +let Entry = require('./models/entrymodel'); |
| 2 | +let Floor= require('./models/floormodel'); |
| 3 | +var express = require('express'); |
| 4 | +const path = require('path'); |
| 5 | +var app = express(); |
| 6 | +const server = '127.0.0.1:27017'; //DB SERVER |
| 7 | +const database = 'test'; //DB NAME |
| 8 | + |
| 9 | + |
| 10 | + //respond with "hello world" when a GET request is made to the homepage |
| 11 | +app.use(express.static(path.join(__dirname, '../build'))); |
| 12 | + |
| 13 | +const port = process.env.PORT || 5000; |
| 14 | + |
| 15 | +// console.log that your server is up and running |
| 16 | +app.listen(port, () => console.log(`Listening on port ${port}`)); |
| 17 | + |
| 18 | +// create a GET route |
| 19 | +app.get('/express_backend', (req, res) => { |
| 20 | + res.send({ express: 'YOUR EXPRESS BACKEND IS CONNECTED TO REACT' }); |
| 21 | +}); |
| 22 | + |
| 23 | +var mongoose = require('mongoose'); |
| 24 | +var mongoDB = `mongodb://${server}/${database}`; |
| 25 | +mongoose.connect(mongoDB, { useNewUrlParser: true }); |
| 26 | +var db = mongoose.connection; |
| 27 | +db.on('error', console.error.bind(console, 'MongoDB connection error:')); |
| 28 | + |
| 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 | + }); |
| 54 | +}); |
0 commit comments