-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (61 loc) · 1.62 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
70
71
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
app.use(express.static(__dirname+'/client'));
app.use(bodyParser.json());
Reservation = require('./models/reservation');
mongoose.connect('mongodb://user:[email protected]:29720/reservations');
var db=mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function(){console.log('DB connected')});
// get all reservations
app.get('/reservations', (req, res) => {
Reservation.getReservations((err, reservations) => {
if(err){
throw err;
}
res.json(reservations);
});
});
//get a single reservation by id
app.get('/reservations/:_id', (req, res) => {
Reservation.getReservation(req.params._id, (err, reservation) => {
if(err){
throw err;
}
res.json(reservation);
});
});
//add a new reservation
app.post('/reservations', (req, res) => {
var reserv = req.body;
Reservation.addReservation(reserv, (err, reserv) => {
if(err){
throw err;
}
res.json(reserv);
});
});
//delete a reservation
app.delete('/reservations/:_id', (req, res) => {
var id = req.params._id;
Reservation.deleteReservation(id, (err, reservation) => {
if(err){
throw err;
}
res.json(reservation);
});
});
//edit a reservation
app.put('/reservations/:_id', (req, res) => {
var x = req.params._id;
var reserv = req.body;
Reservation.updateReservation(x, reserv, {}, (err, reserv) => {
if(err){
throw err;
}
res.json(reserv);
});
});
app.listen(8080);