Aquí tenemos el archivo. json con el contenido podremos visualizar el campo que queramos con el método get usando las id. lo mismo haremos añadiendo mas campos con el método post, modificando los campos existentes con el método put y borrando con el delete. Debemos asegurarnos de haber instalado los paquetes node.js y postman, con los complementos de nodemon
[
{
"id": 1,
"name":"Parking 1",
"type": "AIRPORT",
"city": "ROISSY EN FRANCE"
},
{
"id": 2,
"name":"Parking 2",
"type": "AIRPORT",
"city": "BEAUVAIS"
},
{
"id": 3,
"name":"Parking 3",
"type": "AIRPORT",
"city": "ORLY"
},
{
"id": 4,
"name":"Parking 4",
"type": "AIRPORT",
"city": "NICE"
},
{
"id": 5,
"name":"Parking 5",
"type": "AIRPORT",
"city": "LILLE"
}
]
const express = require('express')
const app = express()
const parkings = require('./parkings.json');
app.use(express.json());
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))
res.status(200).json(parkings)
})
app.listen(8080, (req, res) => {
console.log (`serveur listening : http://localhost:8080`);
})