5 Intro to Backend Development With Nodejs
5 Intro to Backend Development With Nodejs
md 2024-10-27
Why Node.js?
node -v
npm -v
A minimal framework for building web apps and APIs with Node.js.
mkdir my-first-server
cd my-first-server
npm init -y
2. Install Express:
1/3
5_Intro to Backend Development with nodejs.md 2024-10-27
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
node index.js
app.use(express.json());
let users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
];
// GET a user by ID
2/3
5_Intro to Backend Development with nodejs.md 2024-10-27
// DELETE a user by ID
app.delete("/users/:id", (req, res) => {
users = users.filter((u) => u.id != req.params.id);
res.status(204).send();
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
Use tools like Postman to test GET, POST, and DELETE endpoints.
6. Homework Assignment
Extend your API to handle PUT requests for updating users.
Add error handling for when a user isn't found.
3/3