What is the need for Express JS

How to create a simple web server in Node.js.

Node.js has become one of the most popular options for server-side development since it was first released over a decade ago. While it's relatively new to PHP and other backend technologies, it has been widely adopted by technology giants like LinkedIn, PayPal, Netflix, and others.

This article shows you how to use Node.js and the Express.js web framework to build and run your own web server.

Technologies and packages involved

Node.js is a JavaScript runtime based on Chrome's V8 engine that allows you to run JavaScript code outside of the browser. Traditionally, the JavaScript programming language has been used to manipulate the Document Object Model (DOM) to add interactivity to websites.

For this reason, JavaScript code could only be executed in the browser, as the DOM is only present on web pages. You can use Node.js to run JavaScript on the command line and on servers. So it is important to have Node.js and npm installed on your computer before you begin.

On the other hand, Express.js is a minimalist web framework that has become the de facto backend framework for Node.js. However, Express.js is not a requirement. You can still use the built-in http- Use module from Node.js to build your server. Express.js is based on the http- Module and offers a simpler API with all the required configurations.

Create a web server

To better organize your code, you can start by creating a folder that will hold all of the files and dependencies. Since Express.js is not a built-in Node.js module, you need to install it using npm.

Read more: What is npm?

Run the command to install the Express.js package npm install express on your Terminal or Command Prompt. Before installing, make sure you are in the project directory.

When done, you can open the folder with a text editor or IDE of your choice and create a new file named create server.js . To use the Express.js package, you must first have an instance of it in the file server.js import and create.

The main goal of a web server is to respond to requests from different routes with the appropriate handler function. This code processes all GET requests to the root path ( "/" ) and replies with "Hello World!"

Likewise, you can view dynamic content and perform other operations depending on the path and type of request you are making. This can be done using route parameters, through the semicolon designated: before the parameter.

In both of the above examples, the first line represents the use of the .get () Method of Express.js, the two parameters accepted : the endpoint or route and a callback handler function that uses requests and response objects as parameters. These two parameters are automatically sent when you make a request.

In the second line the answer is given via the method .send () for the Response object . You can enter any text or HTML in the brackets. With dynamic routes the access gives up req.params.name (since you /: used name ) of the requirement object the value of the dynamic Route parameters (in this case Surname ) back.

To listen to incoming requests on a port, you can use the .list () that uses the port number and an optional callback function to run on success.

I used port 5000 in the example, but you can change it to any valid port. That's all of the code you need to create a simple web server using Node.js and Express.js. The same concept can be further expanded to include other requirements such as POST , PUT or DELETE to other routes too send . This is how the file looks server.js out:

Testing the server

To run the code and start the server, run the command node server on your terminal or at the command prompt in the project directory. This will perform the callback function that you did for the method .listen () specified .

To confirm the server is working, open a web browser and visit http: // localhost: 5000

If you like a dynamic route visit http: // localhost: 5000 / muo , will be the second Handler function executed and displayed:

To stop the server, press on Windows Ctrl + C or under MacOS Cmd + C.

Node.js can do more

The popularity of JavaScript is skyrocketing as developers use it in both the front end and the back end. This eliminates the need to learn multiple programming languages ​​and allows you to start your journey as a full-stack web developer with just JavaScript.

If you'd rather give Google's programming language a try, building a simple web server is a great starter project.