How to build a basic web server with Node.js

Node.js has been one of the most popular choices for server-side development since its first release over a decade ago. It’s still relatively new compared to PHP and other back-end technologies, but it’s widely adopted by major technology companies such as LinkedIn, PayPal, and Netflix.

This article describes how to build and run your own web server using the Node.js and Express.js web frameworks.

Related technologies and packages

Node.js is a JavaScript runtime built on Chrome’s V8 engine that allows you to execute JavaScript code outside your browser.Traditionally, JavaScript programming languages Document Object Model (DOM), Add interactivity to your website.

Because of this, the DOM only exists in web pages, so JavaScript code was restricted to running only in the browser. Node.js allows you to execute JavaScript on the command line and on the server. Therefore, Install Node.js and npm On your machine before you start.

Express.js, on the other hand, is the minimal web framework that has become the de facto backend framework for Node.js. However, Express.js is not required.Built-in can still be used http A Node.js module for building servers. Express.js http It’s a module that provides a simpler API with all the configurations you need.

Build a web server

To better organize your code, you can start by creating a folder with all your files and dependencies. Express.js is not a built-in Node.js module and must be installed using npm.

read more: What is npm?

Run the command to install the Express.js package npm install express In a terminal or command prompt. Make sure you are in the project directory before installing.

Install Express via npm

When you’re done, you can use your text editor or IDE of your choice to open the folder and create a new file named. server.js.. To use the Express.js package, first import that instance and then server.js Such files:

const express = require('express');
const app = express();

The main purpose of the web server is to respond to requests from different roots using the appropriate handler functions. This code handles all GET requests made to the root (“Https://”) Pass and respond with “Hello World!”.

app.get("", (req, res) => {
res.send('<h1>Hello World</h1>');

Similarly, you can view dynamic content and perform other operations, depending on the path and request type. This can be done using the root parameters indicated by semicolons. : Before the parameter.

app.get('/:name', (req, res) => {
res.send(`<h1>Welcome to ${}!</h1>`);

In both of the above examples, the first line shows the usage of .. get() Express.js methods that take two parameters: an endpoint or root, and a callback handler function that takes request and response objects as parameters. These two parameters are sent automatically when you make a request.

On the second line, the response is .send () The method of the response object. You can enter any text or HTML in parentheses.Access for dynamic routes (Because you used it /:given names(Of the request object) returns the value of the dynamic route parameter (of the request object)given names in this case. )

Finally, to start listening for incoming requests on the port, .. listen() A method that receives a port number to execute when executed successfully and an optional callback function.

app.listen(5000, console.log('Server is running on port 5000'));

This example used port 5000, but you can change it to any valid port. That’s all the code you need to build a basic web server using Node.js and Express.js. The same concept can be extended further to make other requirements such as: Position, Put, Or Delete To another route.Here’s how server.js The file looks like this:

Server test

To run the code and start the server Node server Terminal commands or project directory command prompt. This will execute the callback function provided in. .. listen() Method.

Express server running

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

Express.js-root root demo

Similarly, when accessing the following dynamic route http: // localhost: 5000 / muo, The second handler function is executed and you should see something like this:

Express.js-Demo of dynamic route

Press to stop the server Ctrl + C Windows or Cmd + C On macOS.

Node.js can do even more

The popularity of JavaScript is growing exponentially as developers use it not only on the back end but also on the front end. This eliminates the need to learn multiple programming languages ​​and allows you to start your journey as a full-stack web developer using only JavaScript.

If you want to try out Google’s programming language, building a basic web server is a great starter project.

Featured image of GoWeb server
How to build a basic web server with Go

Ready, Golang: Start building your web server with Go.

Read next

About the author


How to build a basic web server with Node.js

Source link How to build a basic web server with Node.js

Related Articles

Back to top button