-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (57 loc) · 1.35 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
54
55
56
57
58
59
60
61
62
63
const express = require("express");
const app = express();
const cors = require("cors");
const port = process.env.PORT || 3000;
const users = [
{
"id": 1,
"name": "John Doe",
"email": "[email protected]",
"address": "123 Main St, New York, NY"
},
{
"id": 2,
"name": "Jane Smith",
"email": "[email protected]",
"address": "456 Oak St, Los Angeles, CA"
},
{
"id": 3,
"name": "Michael Johnson",
"email": "[email protected]",
"address": "789 Pine St, Chicago, IL"
},
{
"id": 4,
"name": "Emily Davis",
"email": "[email protected]",
"address": "101 Maple Ave, Miami, FL"
},
{
"id": 5,
"name": "William Brown",
"email": "[email protected]",
"address": "202 Cedar Ln, Dallas, TX"
}
];
// Middleware
app.use(cors());
app.use(express.json());
// Routes
app.get("/", (req, res) => {
res.send("Server is running");
});
app.get('/users', (req, res) => {
res.send(users);
});
app.post('/users', (req, res) => {
console.log("POST API working", req.body);
const newUser = req.body;
newUser.id = Date.now();
users.push(newUser);
res.send(newUser);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});