-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (42 loc) · 986 Bytes
/
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
import dummyUsers from "./data.js";
import express from "express";
const app = express();
const PORT = 3000;
app.get("/", (req, res) => {
res.status(200);
res.send("Welcome to Rewards VPN");
res.end();
});
app.get("/users", (req, res) => {
res.status(200);
res.send(dummyUsers);
res.end();
});
app.get("/users/:id", (req, res) => {
const id = req.params.id;
const user = dummyUsers.find((user) => user.id === Number(id));
if (user) {
res.status(200);
res.send(user);
} else {
res.status(404);
res.send({ message: "User not found." });
}
res.end();
});
app.post("/users/add", (req, res) => {
const newUser = {
name: req.body.name,
email: req.body.email,
};
res.status(201);
res.send(newUser);
res.end();
});
app.listen(PORT, (error) => {
if (!error)
console.log(
"Server is Successfully Running, and App is listening on port " + PORT
);
else console.log("Error occurred, server can't start", error);
});