ExpressJS and Back-end Middleware that you should know about…

Nicol Lluesa
Geek Culture
Published in
5 min readJun 8, 2021
Photo by Ian Battaglia on Unsplash

For those of us learning the back-end, things might not make a lot of sense ant the beginning, the structure is completely different to what we’ve learned on the front end and now we’re communicating with the server instead of the client.

Structure in Node is quite straight-forward, you import all the packages and different resources that your project needs at the very beginning, then comes the middleware, which is the software that you want to infuse your code with and then comes the routes that you want to set up. We also need too remember the listen() function, which is crucial to initialize the port and get feedback from it.

In this guide we’re going to focus on the middleware side of the back-end as it is essential once we learn the basics of Node and want to move on to more complex frameworks that would give us more options like ExpressJs.

First, let’s take a closer look at what middleware is, it is defined as “computer software that provides services to software applications beyond those available from the operating system” or in other words, the middleware is a piece of software that has access to both the request and response object and its cycle, therefore, it can execute any code, make changes to the request and the response objects, end the request-response cycle and call the next middleware in the stack.

We can import ExpressJs with a simple line of code:

var express = require(‘express’);

and then initialize it:

const app = express();

with this Express is set up and ready to run in our application and we can now make use of its resourceful middleware, the most commonly used being app.use() function, which is used to mount the specified middleware function(s) at the path which is being specified. It is mostly used to set up middleware for your application.

So let’s say for example that we need to use a middleware function that logs the response method when it is called, then we’ll need to wrap that middleware function inside the app.use() function like this:

app.use('/print', function (req, res, next) {
console.log(res.method);
next();
})

it is important to remark the use of the method next() at the end of it, as it is essential for the function to jump to the next middleware, without this method, only the first middleware function will run and the rest of the routes would be blocked.

Now that we’ve learned to set up express, let’s take a look at the different middleware that allows us to set up our routes on the server, we can easily create a CRUD (Create, Read, Update, Delete) application with just middleware thanks to express and its HTTP functions:

app.get() is going to help use read the GET response from the server.

app.get('/', function (req, res) {
res.send('GET request to the homepage')
})

app.post() will be use to create a new object and add it to the database.

app.post('/', function (req, res) {
res.send('POST request to the homepage')
})

app.put() is the method use to update the data of an existing object.

app.put('/user', function (req, res) {
res.send('Got a PUT request at /user')
})

app.delete() is used to remove an existing object from the database.

app.delete('/user', function (req, res) {
res.send('Got a DELETE request at /user')
})

As seen on the previous example, our routes can have different handler functions, even on the same route. Here’s where routing comes into play as a resourceful tool from Express, routing refers to how an application’s endpoints (URIs) respond to client requests.

A more efficient and clean way of handling our routes is through the use of router, in express, this can be set up simply with the following code:

const router = express.Router();

this tool allows us to set up chainable route handlers for a single route, the Router acts as a module,loads a middleware function in it, defines some routes, and mounts the router module on a path in the main app. It is important to remark that the routes in the main app will have to substitute app for router on its syntax.

We can see how this works in the following example (let’s say this is route.js) :

// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', function (req, res) {
res.send('home page')
})
// define the about route
router.get('/about', function (req, res) {
res.send('About Us')
})

module.exports = router

and now let’s see how this routes would look on our app.js:

var routes = require('./routes.js')

// ...

app.use('/routes', routes)

the function of the router here is giving us access to the route handlers of ‘/routes’ such as ‘/routes/’ and ‘/routes/about’ in a more organized manner only making use of the import/export tools and mounting this middleware with app.use().

Photo by Terry on Unsplash

These concepts can be expanded in greater depth, but I hope that this article helped you grasp a basic understanding on how the backend works and how expressJS is a necessary complement to NodeJS in order to increase code efficiency and functionality through the use of middleware software. As with everything, the more you test and implement these ideas, the better you’ll understand them, so don’t be afraid to dive into them and slowly but surely things will start to make sense.

I’ll see you all on the next article. Happy coding!

--

--

Nicol Lluesa
Geek Culture

Front-End Web Developer who also enjoys the outdoors, martial arts and adrenaline rushes. Check my portfolio at www.nicollluesa.com