What is Express JS?
If you're in the software devlopement field, I'm pretty sure you've heard of the infamous nodeJS framework, express.JS. Express.js is the most popular backend framework for nodeJS. Most developers that use express with node would agree that it makes a night and day difference when working using express rather than without it. Express is most famous for it's ability to make it easier to build web and mobile applications. Some of the things that express does to make things a breeze when developing applications are it's actions that handle templating, routing, and middleware. Keep reading along and I'll go over all of these things in detail to show you how express works.
Middleware
Express middleware is like a swiss-army knife, as it can perform multiples important tasks. It was given the name middleware, because it is usually a series of functions ran in-between the request and response cycle. Middleware functions have the ability to run any code, manipulate request and response objects, stop the response-request cycle, and invoke the next middleware function in the stack. To invoke the next middleware function in the stack, we can call the function, 'next()', inside the current middleware function. One of the most common used functions to run middleware in express.js is app.use() from the app object. App.use() takes in two arguments, one being a path on which the middleware will be invoked. The second argument is the actual middleware function or functions that will be invoked. Here is an example:
var express = require('express');
var app = express();
//Simple request time logger
app.use(function(req, res, next){
console.log("A new request received at " + Date.now());
//This function call is very important. It tells that more processing is
//required for the current request and is in the next middleware
function route handler.
next();
});
app.listen(3000);
Templating
Another special ability that express.js has, is something called templating. Templating in Express is done by what we call, template engines. Template engines convert simple markup into HTML in the clients browser. They allow you to use static template files in your application and whenever the code runs, it fills the template files in with real values. Those files are then sent to the client. Here is a brief example of how express.JS templating works:
//this is an example a template file
html
head
title= title
body
h1= message
//this is an example of the templating process that turns the above code into actual HTML and fill it with the passed in values
app.get('/', (req, res) => {
res.render('index', { title: 'Hey', message: 'Hello there!' })
})
Routing
Routing in express.JS is what defines the path the request will take when ran. When defining a route in express, you can use a method like .get() or .set() and pass a path and a callback function into it. The path is the route that is passed in and is usually an HTTP endpoint. After the path is passed in, the callback function that was also passed in executes and takes the route of the path.
Downsides of Express JS?
Even after all of the great things we've said about express.JS, there are still some downsides about it to consider. One of the biggest cons of express, is it's lack of built-in features like many other frameworks have. It is normal for other frameworks to have things like authentication and authorization already built in. If you would like to have those features in express, you'd have no choice but to add them using third-party libraries. Another potential downside of express.JS is it's lack of structure. With this lack of structure, it's easy to get code jumbled up and almost impossible to read.
Conclusion
Express is a great framework that is used so of often in web application development. It is used at places like IBM, Uber, and Netflix. Just like anything else in life, express has it's pros and it has it's cons. All things considered, I'd still say that learning express will have a positive impact on your career as a software developer. That can be either from increased job opportunities or just simply using it as a lightweight tool in your toolbox. If you were wondering how express works or if it would be good idea to learn it, hopefully this blog clarified some of that for you.
Sources
(https://www.codecademy.com/article/what-is-express-js)
(https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction)
(https://www.simplilearn.com/tutorials/nodejs-tutorial/what-is-express-js#:~:text=Express%20is%20a%20node%20js,helps%20manage%20servers%20and%20routes.)
__
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.