Posted on December 8, 2021 at 09:41 AM
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
To Create Mobile Application, Single page applications or for AJAX calls to get data to render at client end, APIs play a crucial role to fetch from server side with the HTTP utility methods and middleware, creating a robust API is quick and easy in Express JS.
Express provides a thin layer of fundamental web application features, without obscuring Node.js features that you know and love.
To install express js framework we need to setup a new node project with
npm init
Once the project is initialized now we can start installing the express js in the project directory
npm install express –save
As the installation is done you will notice that express with its version is added in dependencies of package.json file.
Now we will set up the entry gate of our project with setting up app.js or index.js whatever you choose at the time of npm init.
In the above screen shot you can have a look that we have setup the entry gate of the app.
So i am explaining this line by line as in
Line 1: we have required the express package in variable.
Line 2: Then we are storing the express core function in the app variable through which we can use the methods of express throughout the file.
Line 3: We need a port to run this application so storing the port 3000 in variable to use in future.
Line 4: We are setting up the first base route and it has a callback function with req and res parameters that help to communicate as to get data and headers in req and same to send the response in res and it is used in line 7.
Line 10: To run this project a port is required to listen to the request and or send the response. So assign the port.
Ok so that was the explanation about the app.js file and to run the application we need to execute some command in terminal here it is
node app.js
In the above screen shot we can see the two consoles that were mentioned in code at line 11 & line 6.
Routing means a path where the user can communicate to get or send data that can be accessed through routing methods such as GET, POST, PUT, DELETE, etc.
Structure of create a route in expressJs
app.Method(PATH, HANDLER Function);
Eg
GET Request
The HTTP GET method requests a representation of the specified resource.
Response
POST Request
The HTTP POST method sends data to the server.
In this we have added a middleware body-parser to receive data from the client end and that can be accessed in req.body. So if you are not aware about middleware it is cover in next section.
Response:
Middleware function are used to add between the path and handler in express route are in express application
app.use((req,res,next)=>{ next() });
In above example of post api urlencodeparser is middleware which help to get post data in req.body
More example for understanding
As in the first image we have created a logger function which is used as a middleware in get request as whenever client requests the get api it will log in console as shown in 2nd image.
Middleware is mostly used for token Validation or changes in req header before the handler function executes.
To access static files in project expressjs have built-in middleware express.static() where we can provide the directory to make static that can be accessed by requesting the file structure.
Here is the example in above screen shot were we have index.html in public folder which we make a static folder so it can be accessed by http://localhost:3000/static/
Template engines convert the template file and data into an html file and send that file to the client side to render.
Need to install the template engine npm i pug in terminal
Setting up the engine
In the above image we have set the view engine of the pug. And in route we have render the pug file by mentioning the file name in the views directory with title variable value and message value.
We need to create a file with extension of pug as we are using pug template engine to render views so it should be in views directory in the root folder. for more details to write pug files visit https://www.npmjs.com/package/pug
=> For Database connection you need a mysql database installed in your system or you need your connection details if you are connecting remotely.
Need to add the code in app.js
For mongodb database connection you need to install mongodb in your system and should have basic knowledge of mongo db collections.
Response:
Source Code URL : https://github.com/nilemandal22/express-js-base-template
Thank You.
We could talk tech all day. But we’d like to do things too,
like everything we’ve been promising out here.