Skip to main content

Creating Routes

What's CRUD?​

It's an acronym that it stands for create, read, update, and delete.And that's basically if you think about any app you've ever used in your life most apps are just, you're either creating something,you're reading something, you're updating something, or you're deleting something.
Pretty much every software you've ever interacted with does those four things,some do them all, some only do some.

Route​

  • For perform the CRUD operations we need to create routes.
  • Routes are the different end-points (Paths) that your API service can listen to and respond to.
  • Each route can have one or more handler functions, which are executed when the route is matched.

A route is a section of Express code that associates an HTTP verb (GET, POST, PUT, DELETE, etc.), a URL path, and a function that is called to handle that pattern.

Requirement​

The resources in this case are the things like the models that we made,the products, the updates, the users, those are resources.
And we wanna be able to do CRUD operations on them.So we wanna create products.We wanna update products.We wanna read products.We wanna delete products.We wanna create updates.We wanna update updates.We wanna read updates.We wanna delete updates, right?So we wanna do CRUD on the resources that we create it.And for each one of those operations, we need a different route.So we need a different method, path combination foreach one of those CRUD operations for every single resource, right.

Create the Route​

Now that we have our schema and data models, we can start creating routes and route handlers to interact with those models. We'll be following a mostly RESTful approach to our API design, but as you'll soon learn, nothing ever stays the course when it comes to REST.

Until some other need presents itself, we want to create a route for every CRUD action for every resource. So, in the case of a Product, we want to create:

GET product/:id    - get a product by a given ID
GET product - get all the products (for an authenticated user)
POST product - create a new product
PUT product/:id - update or replace a product that matches a given ID
DELETE product/:id - delete a product by a give ID
  • First we created a new router using Express.
  • This gives us more flexibility around configuring a set of routes vs. the whole API. You can create as many routers as you'd like with Express and mount them back to the main Express app on the appropriate paths.
  • We then created all the routes for the DB resources we want to interact with.
  • For the handlers, we adding placeholder functions for now. The handler is the function that will be called when the route is matched.
  • If you try to make an API call, your API will get back a 404 status code and some HTML (default 404 response from Express).That's because we didn't mount this router back to the main Express app.
import { Router } from "express";
const router = Router();

// Product

router.get("/product/:id", (req, res) => {});
router.get("/product", (req, res) => {});
router.post("/product", (req, res) => {});
router.put("/product/:id", (req, res) => {});
router.delete("/product/:id", (req, res) => {});

// Update

router.get("/update/:id", (req, res) => {});
router.get("/update", (req, res) => {});
router.post("/update", (req, res) => {});
router.put("/update/:id", (req, res) => {});
router.delete("/update/:id", (req, res) => {});

// Update Points

router.get("/update-point/:id", (req, res) => {});
router.get("/update-point", (req, res) => {});
router.post("/update-point", (req, res) => {});
router.put("/update-point/:id", (req, res) => {});
router.delete("/update-point/:id", (req, res) => {});

export default router;

Importing the Application Router​

you can import above router in the src/server.ts file.

import router from './router'

.......
app.use('/api', router)
  • Import the router from the other file and remove any current route declarations in server.ts
  • We then use something new here:
app.use();

this allows you to apply a router or middleware (we will learn about middleware later) to the entire API, or in our case, to anything using the path /api.

So a route we create in the router like GET /product, is now actually GET /api/product because the router is mounted on the /api path.

You should now be able to hit your API and not get a 404, but, it still won't work. What's happening now is your API is hanging, which just means it never responded back to the request and there is no more code to execute. The client will eventually timeout and close the connection. This happens because we never send a response in any of the handler functions we created. We'll do that soon, but for now, lets talk about middleware.

Testing the API with Thunder Client​

  • Thunder Client is a REST client extension for Visual Studio Code.
  • You can use Thunder Client to test your API routes.
  • You can install Thunder Client from the Visual Studio Code marketplace.

Go to the router.ts file and replace the following code:

router.get("/product", (req, res) => {
res.json({ message: "i am running on the api/product route" });
});

Open the thunder client vs code extension and make a GET request to the following URL:

http://localhost:3001/api/product/
Response
{
"message": "i am running on the api/product route"
}
  • So we have that our stuff is hanging now cuz we don't have any handler, a server should never hang, that is a very, very bad thing.
  • A server also cannot respond twice, so you cannot respond twice to the same request.
Request

So remember those two things, you can't respond twice, and you should always respond once, otherwise, bad things will happen.

Is this possible to generate all the routes dynamically in node.js using configuration file?​

Yes, you can generate all the routes dynamically in node.js using a configuration file. You can create a configuration file that contains all the routes and then import that file in your main file. You can use the fs module to read the configuration file and then create the routes dynamically.

I think I did generate the routes automatically based off like configuration file, so you can do it yourself.

But there's also like really good, like CLI and stuff that will just like to generate it based off of like, a Mongo model or a Postgres model like, or it'll literally connect to your database.And like, I see all the stuff on your database, here are the routes,it'll do that, but there's nothing baked into node that does that.And in fact, that's the thing with node, it is like it actually doesn't have anything because it just relies on the community.Which can be good or bad if the community depends on how, well,the community behaves.

Is the reason why we had to restart our server because TypeScript has to compile?​

So the reason we have to restart our server is because we're not watching for any changes, ts node or even node in general, by default don't watch for changes. The nodemon is the best tool for this, it's a tool that will watch for changes in your files and restart your server automatically when changes are made.

Yes, the reason why we had to restart our server is because TypeScript has to compile. When you make changes to your TypeScript files, you need to recompile them to JavaScript so that the changes can take effect. You can use the tsc command to compile your TypeScript files to JavaScript. You can also use the tsc --watch command to watch for changes to your TypeScript files and automatically recompile them when changes are made.