-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
74 lines (66 loc) · 1.52 KB
/
index.ts
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
import cors from 'cors';
import express, { Request, Response } from 'express';
// Define the user type
interface User {
id: number;
name: string;
email: string;
address: string;
}
const app = express();
const port = process.env.PORT || 3000;
const users: User[] = [
{
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: Request, res: Response) => {
res.send("Server is running");
});
app.get('/users', (req: Request, res: Response) => {
res.json(users);
});
app.post('/users', (req: Request, res: Response) => {
console.log("POST API working", req.body);
const newUser: User = {
id: Date.now(),
...req.body
};
users.push(newUser);
res.json(newUser);
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});