Skip to main content

Express js built-in middleware

Hey!. In this article, we are going to talk about express js built-in middleware. If you are totally new in this, I recommend you check out my last articles on express js middleware by simply navigating to the node js section on this blog.

From the word built-in, you can understand this middleware doesn’t need any installation and hence comes with express by default. On this blog, we are going to look at two different built-in middleware which are express.static () and express.json ().

Today, we will learn about the express.static () middleware. Express static middleware is responsible or used to serve static files or static assets such as a webpage, files, images, style sheets, …

This is very useful in the sense, when ever you include a static asset like images, stylesheet etc in your app, express first checks the folder holding static resources or files.

It is mostly named the “public” folder for best practices and I also think its conventional. Whenever express finds a public folder in your app, it first checks the resources in that folder before coming to the response you sent to a route.

Let’s look at this perfect simple example:

In your project folder, add a folder with name public. In the public folder, add a html file with name index.html. 

server.js

Express js built-in middleware



Express js built-in middleware



OUTPUT
Express js built-in middleware


Express js built-in middleware


1. Notice that on opening the home route, the file, express checks the public folder and serves the html page rather than sending the response in the route handler.

2. Also notice that image source is found in the public folder.

3. Try removing the index.html file from the folder and start the server again, then refresh the page and tell me the results 

Thanks for reading. You can always drop your comments or questions. In our next article, we will learn how to use Express JSON built-in middleware.

Shalom!💖

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...