-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (49 loc) · 1.35 KB
/
Copy pathserver.js
File metadata and controls
65 lines (49 loc) · 1.35 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
const express = require('express');
const app = express();
const PORT = 3000;
// Middleware to parse JSON and form data
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Array to temporarily store users
const users = [];
const spans = [];
// POST endpoint to add a new user
app.post('/api/users/:category', (req, res) => {
// Get route and query parameters
const category = req.params.category;
const status = req.query.status;
// Get data from request body
const { id, name, service, duration } = req.body;
// Validate input
if (!id || !name || !service || !duration) {
return res.status(400).json({
error: 'All fields are required'
});
}
// Create a user object
const user = {
id,
name,
service,
duration,
category,
status,
slow: duration > 12000 ? true : false
};
// Store the user in the array
users.push(user);
// Send response
res.status(201).json({
success: true,
message: `User ${name} stored successfully.`,
receivedData: user
});
});
// GET endpoint to view all stored users
app.get('/api/users', (req, res) => {
res.json(users);
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});