-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
58 lines (46 loc) · 1.35 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
import express, { NextFunction, Request, Response } from "express";
import cors from "cors";
const app = express();
app.use(cors());
//middleware
const userAgent = (req: Request, res: Response, next: NextFunction) => {
console.log("zaman:", Date.now());
next();
};
app.get("/", (req: Request, res: Response) => {
res.send("hello");
});
// ____________________________________________________________
app.get("/user", (req: Request, res: Response) => {
const users = [
{ name: "ali", age: 20 },
{ name: "mmd", age: 21 },
{ name: "hossin", age: 51 },
{ name: "hamed", age: 41 },
];
res.send(users);
});
// __________________________________________________________
app.get("/product", userAgent, (req: Request, res: Response) => {
const products = [];
for (let i = 1; i <= 50; i++) {
products.push({
name: `محصول ${i}`,
inventory: Math.floor(Math.random() * 1000) + 1,
country: `کشور ${String.fromCharCode(
65 + Math.floor(Math.random() * 26)
)}`,
});
}
res.send(products);
});
app.post("/send-otp", userAgent, (req: Request, res: Response) => {
const otp = [];
otp.push(req.body);
console.log(otp);
});
// ______________________________________________________________________________
app.use(userAgent);
app.listen(3000, () => {
console.log("Running Port 3000 Server");
});