Solutions
Are you already looking at this solution? If so, you might as well read it. I'll try to explain my thought process as I go along.
Create the server​
I like to start with a server that uses a single route. This helps me get the server up and running quickly. I can then test that the server is working before I start adding more routes.
In this example, I'm using an environment variable to set the port. This allows me to use a different port when I deploy the app to my production server. I default it to 5050 if the environment variable is not set.
Click here to see the code
import express from "express";
import cors from "cors";
const port = process.env.PORT || 5050;
const app = express();
app.use(cors());
app.get("/", (req, res) => {
const serverStatus = {
status: "running",
port: port,
timestamp: new Date().getTime(),
message: "Welcome to my book API"
};
res.json(serverStatus).status(200);
});
app.listen(port, () => console.log(`Server running on port ${port}`));
You should be able to test it out using curl.
Click here to see the code
curl http://localhost:5050
Add your "database"​
Add a variable that contains all of your entries. In this case, I'm using an array that contains a list of books.
Click here to see the code
const books = [
{
id: 0,
title: "The Alchemist",
author: "Paulo Coelho",
description: "A book about following your dreams",
pages: 163,
currentPage: 163,
read: true
},
{
id: 1,
title: "The Prophet",
author: "Kahlil Gibran",
description: "A story about a prophet",
pages: 107,
currentPage: 23,
read: false
},
{
id: 2,
title: "Thus Spoke Zarathustra",
author: "Friedrich Nietzsche",
description: "This book is about the fictitious travels and speeches of Zarathustra.",
pages: 352,
currentPage: 52,
read: false
}
];
Get all records​
Start with a route that lists all the records. This is usually the easiest, especially when you're using a real database. In this case, we can just return the array of books.
Click here to see the code
app.get("/books", (req, res) => {
res.json(books).status(200);
});
Get a single record​
Next, add a route that returns a single record. In this case, we can use the id to find the book in the array. To keep it simple, the id is the index of the book in the array.
This is not a good idea in a real application. You should use a unique id that is not the index of the array. Otherwise, if you delete a record, the index will change and you'll have to update all the other records.
Click here to see the code
app.get("/books/:id", (req, res) => {
const book = books[parseInt(req.params.id)];
res.json(book).status(200);
});
Add a record​
Now, let's add a route that allows us to add a record. In this case, we'll just add the book to the array. The id will be the length of the array.
Again, this is not a good idea in a real application. Once your start deleting records, the id will no longer be the length of the array.
Click here to see the code
app.post("/books", (req, res) => {
let book = req.body;
books.id = books.length;
books.push(book);
res.json(book).status(201);
});
Update a record​
Next, let's add a route that allows us to update a record. In this case, we'll just replace the book in the array with the new book.
Click here to see the code
app.put("/books/:id", (req, res) => {
let book = req.body;
books[parseInt(req.params.id)] = book;
res.json(book).status(200);
});
Delete a record​
Finally, let's add a route that allows us to delete a record. In this case, we'll just remove the book from the array.
In case I haven't emphasized this enough yet, this is not a good idea in a real application. You should use a unique id that is not the index of the array. Otherwise, if you delete a record, the index will change and you'll have to update all the other records, which will cause all sorts of other issues.
Click here to see the code
app.delete("/books/:id", (req, res) => {
books.splice(parseInt(req.params.id), 1);
res.status(204).send();
});