When building web apps using Node.js, setting up a server might take a long time. Because of the community's support, Node.js has matured enough over time.
Using Node.js as a backend for web apps and websites allows developers to get started quickly on their project.
In this article, we'll look at Express, a Node.js web development framework that includes capabilities like routing and rendering, as well as support for REST APIs.
What is Express?
Express is the most popular Node.js framework since it takes little setup to begin developing an application or API and is both quick and agnostic.
In other words, unlike Rails and Django, it does not enforce its own concept that an application or API should be created in a certain way.
The amount of npm modules accessible, which makes it pluggable at the same time, may be used to determine its flexibility.
If you have a basic understanding of HTML, CSS, and JavaScript, as well as how Node.js works in general, you'll be able to get started with Express in no time.
TJ Holowaychuk created Express, which is currently maintained by the Node.js foundation and open source contributors.
To get started with Express programming, you'll need to install Node.js and npm.
You can install Node.js on your local workstation, and it comes with the command-line program npm, which will enable us later in our project install plugins or dependencies.
Please open your terminal and type: to see whether everything is installed successfully.
If you see the version number instead of an error, it implies Node.js and npm were successfully installed.
Why use Express?
Before we go into the mechanics of utilizing Express as a backend framework, let's take a look at why we should use it and why it's so popular.
You can use Express to create single-page, multi-page, and hybrid web and mobile apps.
Another popular backend use is to offer a client with an API (whether web or mobile).
It has a default template engine, Jade, that aids in the flow of data into a website structure while also supporting different template engines.
It supports MVC (Model-View-Controller), a widely used web application architecture.
It is multi-platform and not restricted to a single operating system.
It uses the single threaded and asynchronous paradigm of Node.js.
Creating package.json
Every detail about each Express project is included in a JSON (JavaScript Object Notation) file.
The number of installed modules, the project's name, the version, and other meta data.
To include Express as a module in our project, we must first establish a project directory, then a package.json file.
This will create a package.json file at the project directory's root. The package.json file must exist in that directory in order to install any npm module.
Installing Express
Now that we have the package.json file, we can execute the following to install Express:
We can verify that Express has been installed appropriately in two ways. First, the package will have a new part. Our Express is listed in a json file called dependencies:
The second method is that in the root of our project directory, a new folder named node modules appears out of nowhere.
This folder contains the packages that we install in our project locally.
Building a Server with Express
We'll build the index.js file at the base of our project's directory to leverage our Express framework package and establish a simple server application.
Go to your terminal and type: to start the server
The server will now be started. On port 3000, this bare-bones application will listen.
Our browser sends a request to http://localhost:3000, and our server responds with Hello World, with the browser acting as the client and the message shown there.
The need function is used in the first line of our code to include the express module. This is how we include and utilize a npm package in any JavaScript file in our project.
We need to create an instance of Express before we can use it to process requests and responses from the server to the client. It's the variable app in our situation.
When a get request at the provided URL is called, app.get() instructs the server what to perform.
It has a callback function (req, res) that listens for incoming requests and responds appropriately with the res response object.
The Express framework makes both req and res available to us.
The request query string, arguments, content, and HTTP headers are all attributes of the req object, which represents the HTTP request.
When an HTTP request is received, the res object represents the HTTP response that an Express app provides. In our scenario,
whenever a request to the route / is performed, we send a text Hello World.
Finally, app.listen() is the method that starts a port and host, in this instance localhost, for connections to wait for incoming client requests.
We may use a number like 3000 to define the port.
Comments
Post a Comment