Express.js, or simply Express, is a web application framework for Node.js, released as free and open-source software. It is designed for building web applications and APIs. It is in fact the standard server framework for Node.js.
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. It is an open source framework developed and maintained by the Node.js foundation. Express provides a minimal interface to build our applications. It provides us the tools that are required to build our app. It is flexible as there are numerous modules available on npm, which can be directly plugged into Express.
There are two ways to install a package using npm: globally and locally.
- Globally − This method is generally used to install development tools and CLI based packages. To install a package globally, use the following code.
- Locally − This method is generally used to install frameworks and libraries. A locally installed package can be used only within the directory it is installed. To install a package locally, use the same command as above without the -g flag.
The first line imports Express in our file, we have access to it through the variable Express. We use it to create an application and assign it to var app.
This function tells what to do when a get request at the given route is called. The callback function has 2 parameters, request(req) and response(res). The request object(req) represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, etc. Similarly, the response object represents the HTTP response that the Express app sends when it receives an HTTP request.
This function takes an object as input and it sends this to the requesting client. Here we are sending the string “Hello World!”.
app.listen(port, [host], [backlog], [callback]])
This function binds and listens for connections on the specified host and port. Port is the only required parameter here.
The above mentioned is a brief about Express.js. Watch out this space for more information about the latest trends in Technology.