Getting started with ExpressJS
Chapters
This course is for beginners who wants to get started with ExpressJS framework for NodeJS. This course is subdivided into 13 parts as listed below
- Welcome note
- Installing NodeJS and ExpressJS
- Developing your first application with ExpressJS
- Working with Views/Templates
- Handling requests
- Application specific settings
- Midlleware and request flow
- Understanding routes
- Modularizing routes in ExpressJS
- Request Object
- Response Object
- A word on locals
- Conclusion
Please let us know how we can do better next time by leaving your feedback.
Author: Subject Coach
Added on: 5th Mar 2015
Please get in touch with your teacher or tutor in case you have a question related to this lesson
None just yet!
Express application is actually a series of middleware calls, what this means is that, when a request is received by the flow of request would start from top of the file, loads initial application specific settings, and then move down to load middlewares, then processing whatever is left in the process queue, such as route handling etc.
Middlewares is able to call next middleware in the stack, the are also capable of ending the request and response cycle if call to next middleware is not made, Middlewares are quite able to update the request and response objects. You can make use of middlewares to execute any kind of code higher up the request stack. Remember that request flow starts right at the top of your application file and comes right to the bottom unless request or response cycle was ended before.
There are roughly 4 kinds of middlewares, Application-level middleware, Router-level middleware, Third-party middleware and Built-in middleware.
An Application-level middleware are actually bound to express object, that is the express app instance itself. Express use the "use" method to allow using middlewares, a middleware can be a function that you wrote, a function that takes a request, response and the callback function as parameters and have some logic inside. Let's create such middleware. My middleware function here takes a request, response and the callback function as parameters, and outputs "App level" to console, and finally calls next() middleware in the stack. I should move this function above my routes, because request flow is from top to bottom, and middlewares have to do their thing before actual route is being hit. I can update middleware mount function "use", to work on specific route to say, user/:name.
Router level middleware works just like application level middleware, except they are bound to an instance of express.Router(). So I will create an instance of Router object, and instead of calling the "use" function for express instance, here I will use it upon a router instance, specify my router path and then my middleware function. One thing to note here is that these middleware functions are no different to what we see in VERB methods such as GET or POST.
Express used to come with a lot of middleware built in, such as the one we saw before with the body parser module, and it used to depend on connect to assist loading these middlewares. As of 4, Express no longer depends on Connect. Except for express.static, all of Express’ previously included middleware are now maintained separately. Express.static is based on serve-static, and is used for serving the static assets such as CSS files or JavaScript files for an Express application. Here I am just telling the static method where to find project static files, which are in my public folder.
Third-party middleware are something that other developers maintain and are generally available through NPM install. In the previous video, I showed you how you can make use of body parser middleware to parser request body into key value pairs. Here I am logging the value of request body which will be filled by body parser middleware. When you go to the resources section of Expressjs.com and then goto the middleware section, you will see a lot of middlewares that are commonly used with Express, and you can use out of the box. There seems to be nothing left that you cannot find there or in NPM repositories in general.
Middlewares offers flexibility for a developer to obtain full control over the request stack. One must know that the way request is handled within an express, starts from top, which is the mount point and go all the way to the bottom. We have covered middlewares for express version 4. Its no different to version 3 but only difference is that in Express version 3 many middlewares were built in, and to use them now in version 4, you have to install them using NPM.
In next video, I will cover routes.