-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (92 loc) · 2.52 KB
/
Copy pathserver.js
File metadata and controls
112 lines (92 loc) · 2.52 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const cors = require('cors');
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
let vehicles = [
{
id: 1,
make: "Honda",
model: "Accord",
year: 2020,
color: "grey",
plateNumber: "ES6 3G6",
vin: "1G1ZT51806F128009"
},
{
id: 2,
make: "Infiniti",
model: "G37",
year: 2009,
color: "blue",
plateNumber: "RW4 993",
vin: "JH4KA3240HC002301"
},
{
id: 3,
make: "Porsche",
model: "Macan",
year: 2019,
color: "red",
plateNumber: "6HG 2ER",
vin: "JH4KA3160KC018606"
}
];
app.use(express.static("public"));
app.use(cors());
// Configuring body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.listen(3000, () => {
console.log("Server running on port 3000");
});
app.get("/vehicles", (req, res) => {
return res.send(vehicles);
});
app.get("/vehicles/:vehicleId", (req, res) => {
return res.send(
vehicles.find((vehicle) => vehicle.id == req.params.vehicleId)
);
});
app.post("/vehicles", (req, res) => {
const vehicle = req.body;
vehicles.push(vehicle);
// output the vehicle to the console for debugging
console.log(vehicles);
return res.send("Vehicle has been added to inventory");
});
app.post("/vehicles/:vehicleId", (req, res) => {
return modifyVehicle(req, res);
});
app.put("/vehicles/:vehicleId", (req, res) => {
return modifyVehicle(req, res);
});
app.delete("/vehicles/:vehicleId", (req, res) => {
// Read the vehicleId from the URL
const vehicleId = req.params.vehicleId;
const vehicleIndex = vehicles.findIndex((vehicle) => vehicle.id == vehicleId);
if (vehicleIndex === -1) {
return res.status(404).send(`No vehicle with ID ${vehicleId}!`);
} else {
vehicles.splice(vehicleIndex, 1);
// output the vehicle to the console for debugging
console.log(vehicles);
return res.send(`Vehicle ${vehicleId} has been deleted.`);
}
});
function modifyVehicle(req, res) {
// Read the vehicleId from the URL
const vehicleId = req.params.vehicleId;
console.log(vehicleId);
const newVehicle = req.body;
const vehicleIndex = vehicles.findIndex(
(vehicle) => vehicle.id == vehicleId
);
if (vehicleIndex === -1) {
return res.status(404).send(`No vehicle with ID ${vehicleId}!`);
} else {
vehicles[vehicleIndex] = newVehicle;
// output the vehicle to the console for debugging
console.log(vehicles);
return res.send(`Vehicle ${vehicleId} has been updated via ${req.method}.`);
}
}