> For the complete documentation index, see [llms.txt](https://smu-portal.gitbook.io/get-started/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://smu-portal.gitbook.io/get-started/book-module-tutorial/backend/create-route.md).

# Create Route

Creating routes is a fairly straightforward process, we start by adding a new class `book.js` in the `/routes/api`folder.

![](/files/-ML9Xzt2Ne9Q8DJ9HkfV)

Then, we need to import the book service, book model and router.

```javascript
const Router = require("express").Router;
const bookService = require("../../services/book.service")();
const Book = require("../../models/Book");
```

We should also add the following line:

```javascript
const router = Router({
  mergeParams: true,
});
```

This allows us to inherit the parameters defined in `req.params` at the parent router.

Now, all we have to do is define our API endpoints. Let's create a route that will get us all the books:

```javascript
router.get("/getBooks", async(req, res) => {
  try {
    const books = await bookService.getBooks();
    res.send(books);
  } catch(err) {
    res.json({ success: false, msg: "Failed to get books"});
  }
});
```

Since we need to retrieve the data about all the books registered in the database, we create an HTTP request with a **GET** method. The URL `/getBooks` is our API path.

{% hint style="warning" %}
**Caution:** `/getBooks`is not the complete API path. In this case the complete API path is [`http://localhost:3000/api/user/getBooks`](http://localhost:3000/api/user/getBooks)

In fact, our newly created API path is appended to a previously defined path at the parent router. This router is located in the root directory in`app.js.`
{% endhint %}

Next, we add two more routes, one for adding a book and another one to delete a book:

```javascript
//Route to create a book
router.post("/addBook", async(req, res, next) => {
    try {
        const {title, author, isbn} = req.body;
        await bookService.addBook(title, author, isbn);
        res.send({ success: true, msg: "Book Added"});
    } catch (err) {
        res.send({ success: false, msg: "Book not Added!", err})
    }
});
```

Here, our HTTP request method is a **POST** method since we will be sending data rather than retrieving it. We extract the title, author and ISBN from the request body. Then, we create an instance of a book with those attributes and we call the `addBook()` function that we defined in our book service. If the request succeeds, we will respond with the message "Book added" otherwise "Book not added!" as well as attach a success indicator (a boolean variable in our case).

```javascript
// Route to delete a book
router.delete("/deleteBook/:isbn", async(req, res) => {
  try {
    const isbn = req.params.isbn;
    bookService.deleteBook(isbn);
    res.send({ success: true, msg: "Book deleted"})
  } catch (error) {
    res.send({ success: false, msg: "Book not added!"})
  }
});
```

In this function, we changed the method of request is set to **DELETE** as we will be removing an entry from the database. This time, we extract the ISBN from the path rather than the body.

As always, we finish by exporting our router.

```javascript
module.exports = router;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://smu-portal.gitbook.io/get-started/book-module-tutorial/backend/create-route.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
