Tagged: 15
- This topic is empty.
-
AuthorPosts
-
July 4, 2023 at 3:50 am #783617darcydowns3549Participant
Express framework in node.js
We need to also know about the express.js framework which is the basic building block for writing strong, lightweight, scalable and powerful REST API’sExpressjs is a popular tool in developer circles because :Express.js is a JavaScript framework, JavaScript as we have discussed earlier, it is easy to understand, has a large community to reach out to. The Express framework in node.js makes writing backend code for web applications easier. Express.js also gifts our application with flexible middleware, which executes during the lifecycle o a request to the Express server. Express.js is a third party module which is needed to be imported into our application.
Express is fairly minimalist, but you can develop compatible middleware packages to address most web development needs.
Express is the most popular Node web framework, and it is the underlying technology on which many other Node web frameworks are built on. According to online database, Express is a standard implementation of the Model-view-controller program pattern, and includes internal middleware modules for error handling.
You can find the complete developer document and manual over here https://expressjs.com/en/starter/installing.html
There are a lot of packages that complement Express, including more than one that can authenticate users (for login, sessions, user logins, cookies, handling forms, etc.)
Express provides, a minimalistic light web layer, without obscuring Node.js features that you already know – and love.
With a multitude of HTTP utility methods – as well as the middleware available to your end-users, the creation of robust APIs is quick and easy.
Also provides a robust set of features so that web and mobile applications operate well.
Additionally Express is able to perform –
Placing middlewares near the beginning of the request pipeline where additional logic can be performed.
Write request handlers(you can learn about HTTP request response cycle here) to handle HTTP requests for different HTTP verbs(you can learn about HTTP verbs here) at various locations in the request processing pipeline.
We can install express in the hello world program we developed earlier and write that program in a following way –
First install express into your nodejs package.
npm i —save express
Note that we write every time “—save” to save the express or any nodejs package as a global package.
A global package will mean that the module will be installed in whole operating system meaning you don’t need to reinstall this same package every time for each different API/program you design.
If you don’t want to save it globally then simply remove this “—save” from the install command.
const express = require(‘express’);
const app = express();
app.get(‘/’, function(req, resp)
resp.send(‘Hello, World!’);
);app.listen(8080, function()
console.log(‘Server listening on localhost ‘ + 8080);
);
Note that previously we needed to require the http module, and also didn’t required to write the content – type, which express by default does on its own.This also lead us to create a clean minimalistic code. We simply required the express module, (express which is a third party module, learn about types of nodejs modules here)
We then created an app instance containing the inbuilt functions of the express module.
Then we defined a get request which will respond the the incoming request and the response to be served is defined by the function containing “req” variable as request which contains all the parameters which were sent in as the request and a “resp” variable which will define what will be the response sent to the corresponding request. In this case
we sent a simple response of displaying “Hello World” on the localhost port 8080.
Also we took a get HTTP request for the default localhost:8080 URL, which means when we type
“localhost:8080” on our web browser the default web page to be displayed is the text “Hello World”, we wrote this response by defining the app.get function for the default URL(‘/‘). By specifying ‘/‘ we are instructing the program that what will be the response that should be sent when a user wants to access the homepage.
Consider when we type google.com in the browser, the Homepage of the google is then displayed. In that similar way when we want to define the homepage of a given domain and in our case we need to specify the get request handling the ‘/‘ route or URL.
Then we should save the file with a .js extension and run the program by the command “node filename.js” (replace the filename with the name of the file you save it with). This process is defined in the previous section and you can access it over here towards the end of the page.
When you loved this short article in addition to you want to get more info relating to How to write a basic computer program kindly go to the web-site.
-
AuthorPosts
- You must be logged in to reply to this topic.