The Story of a Backend: Paving the Paths
Well, welcome to the next part of this wonderful journey we are taking through the depths of the backend development process. Uptill now, we have talked about setting a node project and get talking to a database (which will be a sql database for us). Wait.. have you read the first two parts? No? well here is the link to the part1(The beginning) and part2(the discussion over databases).
Oh, links reminds me.. today the target is to make endpoints and do a bunch of routing to guide our requests through the jungle of paths to get what they desire. For all the routing and app creation, we will be using a very popular web framework for nodeJS — Express(we installed that in the part 1 itself😬). Before we jump into a bunch of cool code, Let’s first talk the talk of express and understand what actually is happening and why I am doing what I am doing.. Let’s go!
Express is an amazing framework with not much of it’s own functionality, the application can be thought of as a series of function calls. Middlewares are the functions we run for achieving a certain functionality. To use a function in the app, we use the use() method(will show up a lot of times).
There are three kinds of middleware we use in express ..
- > application middleware : the application level middleware is added to the app using the app.use() or app.REST_VERB(). where REST_VERB can be get, post, put etc. An example of the same would be following..
Note: If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.
let app be an express instance,
now this is just a function example that just displays current time on the console. we import the express functions from express . We instantiate an express application and add the middleware to the app. Notice the lack of any specific route when to run the middleware.
If we want too attach it to a path, the code would looks something like this
- > route middleware: a route middleware is added to a certain route. This is the most popularly seen middleware for our purpose. We use express router to create routing for the application and to attach a middleware to the router. an example of such a middleware is as below
- > third party middleware: this provides some additional functionalities to the app but saves us the pain to create all the code from scratch and think of all the handlings.. some example for such middleware , we will use are..
- morgan — used for logging
- helmet — Helmet helps you secure your Express apps by setting various HTTP headers. It’s not a silver bullet, but it can help.(used for production environment)
- cookie-parser — Parse
Cookieheader and populate
req.cookieswith an object keyed by the cookie names
an example of adding a third party middleware is as follows
- > error handling middleware — Define error-handling middleware functions in the same way as other middleware functions, except with four arguments instead of three, specifically with the signature
(err, req, res, next)
- > built-in middleware — The middleware functions that were previously included with Express are now in separate modules. following is the example of such middleware
- express.static: serves static assets such as HTML files, images, and so on.
- express.json: parses incoming requests with JSON payloads.(available with express 4.16.0+)
- express.urlencoded: parses incoming requests with URL-encoded payloads.(available with express 4.26.0+)
following is an example of adding built-in middleware
Now that, that is out of the way, Lets talk about how to set us some routes using Express router.
The interesting thing about being able to create routers like this is that we can create a router, add middleware to the routes and pass on the router to the app and we maintain the modularity ..
We create the folder routes for storing the routers and exporting them form there.
Here is a sample route file code
The routes are all pulled into the indedx.ts file from where they can all be made accessible to the app
Okay… So, We have, now seen how to start with a node application, we have talked a bit about the databases and how to connect the nodeJS to the SQL database and if I was successful in giving a good demo, we also know how to add middleware to the app and get routing in express.
With the next blog we will wrap up this story with a detailed information of creating a nodeJS based server connecting the app to a database.
Until then.. Have fun, take care and chao!