Created
March 19, 2024 17:44
-
-
Save BrianJenney/c3e6bf7b754ac03e750265f304d3a59a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JOIN Parsity.io amigo | |
// a coding program where we teach you to build software, work with experts and get coached towards your first (software) job! | |
// 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}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment