The course is part of this learning path
This Tech Talk, hosted by Stefano Cascavilla, one of our lab content creators, introduces ExpressJS, a lightweight framework based on Node.js, which can be used for launching web applications and RESTful APIs. In this course, you will learn about what the ExpressJS framework is, how it works, and how you can use it for your applications. You'll learn how to create and implement middleware using ExpressJS, and you'll also learn about the Mustache template engine.
Learning Objectives
- Obtain a foundational understanding of ExpressJS
- Learn how to create and implement middleware
- Learn about the Mustache template engine
Intended Audience
This course is intended for developers or anyone who wants to improve their knowledge of ExpressJS.
Prerequisites
To get the most out of this course, you should have a basic understanding of JavaScript and Node.js.
Hello everybody, my name is Stefano Cascavilla and I'm one of the labs content creator, here at the Cloud Academy. And today I'm here along with the Logan Rakai and Andrew Burchill. And I will have a tech talk about the ExpressJS framework. So let's get started.
What is ExpressJS? ExpressJS is a NodeJS fast and lightweight framework and of course it is a very lightweight because it contains and it supports the core components and core parts via the Web Application, RESTful APIs, and nowadays also GraphQL APIs. But for example, as we'll see it doesn't have a few parts that some of the operators think that are essential. So for example ORM, object-relational mapping.
It can be used to build and then launch both web applications, RESTful APIs because of the new official and also unofficial modules and libraries developed by third parties. There is now the possibility also to create the GraphQL APIs, so to start a graph QL server. It is a very useful and very powerful framework. And especially for the web applications there is a template engine integration and we can leverage the template engine if we prefer, for example, mustache or the bug one and to build and to render dynamic templates. So we can create an for example, steady parts of HTML and then we can handle sending variables declaring and using ISA statements for loops and while loops. And it provides the core components needed to use and to handle the main parts of an application.
So both application RESTful and GraphQL such as route handler and middleware integration and the of course template engine integration. And it is the base for other famous, no JS frameworks such as LoopBack. So loads of frameworks and also external models and the libraries are based on ExpressJS especially because of its popularity and developer community.
So for a full list of frameworks and libraries that are based on the ExpressJS framework you can reach out to the, the official documentation of ExpressJS on the website. So you can have a full list of this. So what are the core concepts of ExpressJS? The first one is the application reference which is very, very important and it is the entry point for everything, then we have routes and the router. We can also have multiple rulers for a single ExpressJS application. We then have middleware, so we can have both custom, so we create the middleware, and we have also third-party middleware, so we can install external, official and unofficial models, libraries, and it can leverage them, as in middleware. And then we have template engines.
So let's start from the application reference. Every time you want to start an Express application, you need to first declare the app. You import the Express module and you instantiate it by using this kind of syntax and usually the application reference is the app. And from the app one, you can perform everything.
So you can define routes, you can integrate components, you can integrate routers, and then you can also start the server. And of course, after all the configurations and integrations you make in your ExpressJS application, you need to remind and you need to use this reference to start the ExpressJS server.
This is something that we can miss, but it is very important to meet because if you don't do it, we can create everything but when we start and when we try to start a new JS script, it simply won't start. So we can use and we need to use the listen method, specifying the port. So for example, 8080, or 3000 and we also have the possibility to define, we can leave it empty, of course, but usually the kind of log, server log is simply say, "Server is listening..." So this function and this call back executing after the ExpressJS server has been correctly started.
So one of the most interesting and useful parts, especially for a restful API route, so we want to define each one to path. You need to use routes and routers. And in this case, what is a route? A route is simply a tuple. So you need to specify, sorry a tuple, you need to specify the HTTP method, so get, post, route, delete.
You need to specify then the path, that's what you want. For example, in this case /first or /second. And then you need to define a call back which is the logic flow that will be implicated after the root is triggered. There is a statement where you define an HTTP method to reach the root and a path. Then you're assessing the logic flow as a call back and the parameters are two. So request and the response.
The request represents the object request, so for example, you can use it to access the region, for example, with a parameter, the question parameters, or if you're handling a post, a route, or a delete, you can access the body that the client sent. And the router is an Express entity, that groups some roots, so suppose your application is gonna be and you have different paths and locations for your application.
So if you have an entity A, an entity B, and an entity C, and you want to better organize these end points associated to these entities. And of course you want also to define namespaces because for example, two of the entities, I suppose A and B, share the end point /home, ok? So you want to duplicate these endpoint, but if you do it by only using the, by not using the router and only using the routes directly from the app reference, of course, you can't do it. At least if you don't specify different HTTP methods for example, you cannot associate in the /home end point to get routes. That can not be done.
So this case, we need to use a router. And because we didn't find a namespace, in this case, we can then export the router. In this example, we have everything in the same server JS file. Another example: you could have created a router.JS file creating two, three, or four routes and then exporting this router. And then we can import the router in the main file. So where we had the application reference and you see it as a middleware specifying a prefix.
In this case, we are using the use method specifying the prefix for this router, and then we specify the router. In this case, we can have for example, /a /hold, and then /b /hold. So namespace allows us to create two equals end points but with different logic and different parts of the application. And what about middleware ExpressJS? It's a very flexible framework. So it allows us to create our custom middleware, so we can create functions that have three parameters: request, response, next.
Why request and response? Well, because the middleware can access the request parameters meters and can also, for example, leverage and use inset response parameters before the core logic flow is executed. And then the next parameter is very important. And it represents a callback that we absolutely need to call after the logic rated to a middleware has been completed because it lets the application proceeds through executing the last part of the application.
In this case, for example if we integrate general middleware into this route, if we don't call the next here, then the logic executes the console log. So this is a middleware but then it doesn't execute the logic part related to the route. And as you can see from these images, we can also integrate them on a single route, so in a single part of the application by only specifying the middleware between the end point of the root and the callback of the route.
But suppose we want to nintegrate for multiple routes. So for example, you are creating an authentication and authorization middleware, and you have um, I don't know, 10 routes that need to be authenticated and authorized. Of course you can set for each of them the middleware specification in the, in assign the route but a very useful and simple use case is to leverage the middleware by using the user-needed reference, the application reference.
So app.use, and then specify the name of the function. So the name of the middleware that we want to use and all the parts. In this case, the routes that are declared after the middleware, will automatically integrate the middleware. For example, if we define the middleware as an application one and then we set a post slash second route after the integration of middleware. If we don't authenticate, we can not go with this. This equals for example, we can set an error message. And, also the plain template engines as stated at the beginning, Express can integrate also a front end tier. And because of that, it offers template engine integration essential to build and answer dynamic templates.
There are different template engines so that you can use in Express. And one of the most popular and most used and also easy to use is Mustache. Well, the general part that you need to follow and need to performance when you want to integrate it into your ExpressJS application is composed by these rows. You need to set the, the directory. So the views directory where you will holster all of the templates, okay, that you are creating. For example, Mustache files. Then you have to specify the name of the template engine that you are, that you want to use. So in this case, Mustache, and then you have to pass the actual reference in the template engine that you are using.
In this case, we have installed the Mustache module. We have imported it into the server.JS file. The main file, the application, and then by using the engine method of the upper reference, we have defined that we are using Mustache and that we have passed the reference into the template engine and well, the usage, as you can see here is very simple.
You can leverage the response object, render, the dynamic template that you want to use. You need to specify the name of the complaint and automatically express that we look for a test.mustache in this case, under this directory, so /use and we'll run this template and this is the simple code of the test template that we have created, and we are using two variables.
By using these two parenthises, we are using two variables. And of course we need to specify server side the values for these two variables, so name and day of week in this example. Of course, we can also insert if statements, if.statements, loops, and everything else that sets our template dynamic.
So these are the core parts and genetic parts that ExpressJS has, but one of the questions is "Is it too much light?" Actually, it could be, because as you can see the core components are essential, of course lots of things are missing such as an ORM, object relational mapping, and authorization and authentication system. And also for example, a solution for if you are, for example, building a web application for handling user sessions or cookies. So these parts are not natively integrated into ExpressJS.
Because of its popularity, lots of external components can be integrated by using official but also unofficial end game modules and libraries. You can easily find thousands of modules and libraries to extend the functionalities and features of your ExpressJS application. And that means that of course, ExpressJS is a lightweight framework but it is also super, super extensible.
You can start it easily to create, for example, a tutorial or a guided application, maybe composed by only two routes, but you can scale easily to an enterprise or production application by integrating third-party modules. And of course, this is a requirement because of its simplicity, if you are thinking to build a production-ready application, maybe you don't want only to use a custom, sorry, native components, but want to extend them by using library.
So a good thing for skilled developers because maybe they don't want to have loads of schemas to follow and constraints but that's good maybe for beginners, beginner developers, or maybe developers that are not confident with the JavaScript or with the ExpressJS framework. Because if we compare it, for example, with the django framework for Python, of course, django framework is more strict, but offers also loads of managed components that the user or the developer can easily integrate and use.
So this is the main difference between ExpressJS and the frameworks that are not as light as ExpressJS but offers loads of components built in.
Thank you so much for your attention and thank you for attending. I hope you enjoyed it. If you have any kind of question, feel free to ask me.
Stefano studies Computer Science and is passionate about technology. He loves working with Cloud services and learning all the best practices for them. Google Cloud Platform and Amazon Web Services are the cloud providers he prefers. He is a Google Cloud Certified Associate Cloud Engineer. Node.js is the programming language he always uses to code. When he's not involved in studying or working, Stefano loves riding his motorbike and exploring new places.