-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (44 loc) · 1.34 KB
/
index.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
const express = require('express')
const app = express()
const parkings = require('./parkings.json')
/**
* Import MongoClient & connexion à la DB
*/
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';
const dbName = 'parkingApi';
let db
MongoClient.connect(url, function(err, client) {
console.log("Connected successfully to server");
db = client.db(dbName);
});
app.use(express.json())
app.get('/parkings', (req,res) => {
res.status(200).json(parkings)
})
app.get('/parkings/:id', (req,res) => {
const id = parseInt(req.params.id)
const parking = parkings.find(parking => parking.id === id)
res.status(200).json(parking)
})
app.post('/parkings', (req,res) => {
parkings.push(req.body)
res.status(200).json(parkings)
})
app.put('/parkings/:id', (req,res) => {
const id = parseInt(req.params.id)
let parking = parkings.find(parking => parking.id === id)
parking.name =req.body.name,
parking.city =req.body.city,
parking.type =req.body.type,
res.status(200).json(parking)
})
app.delete('/parkings/:id', (req,res) => {
const id = parseInt(req.params.id)
let parking = parkings.find(parking => parking.id === id)
parkings.splice(parkings.indexOf(parking),1)
res.status(200).json(parkings)
})
app.listen(3000, () => {
console.log("Serveur à l'écoute")
})