-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (32 loc) · 873 Bytes
/
server.js
File metadata and controls
38 lines (32 loc) · 873 Bytes
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
// Import express module
const express = require('express');
const cors = require('cors');
//whitelist all the domains
// Create an express application
const app = express();
app.use(cors());
// Define the port number
const PORT = 8000;
const users = [
{ id: 1, name: 'John Doe' },
{ id: 2, name: 'Jane Doe' },
{ id: 3, name: 'Jim Doe' },
];
// Define a route for GET requests to the root URL ('/')
app.get('/:id', (req, res) => {
const id = req.params.id; //cats rats bats dogs 1 2 3
const user = users.find((user) => user.id === parseInt(id));
// Send back some JSON data when this route is accessed
if (!user) {
return res.status(404).json({
message: 'User not found',
});
}
res.json({
user,
});
});
// Start the server and listen on the specified port
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});