How to create 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 big tech companies like LinkedIn, PayPal, and Netflix.

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

Associated technologies and packages

Node.js is a JavaScript runtime built on Chrome’s V8 engine that allows you to run JavaScript code outside of 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 has been limited to executing only in the browser. Node.js allows you to run 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. The integrated module can still be used http A Node.js module to create servers. Express.js http It is 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 a command prompt. Make sure you are in the project directory before installation.

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, import this instance first, 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 management functions. This code handles all GET requests made at the root (“Https://www.makeuseof.com/”) Go through and respond with “Hello World!”.

app.get("https://www.makeuseof.com/", (req, res) => {
res.send('

Hello World

');
});

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

app.get('/:name', (req, res) => {
res.send(`

Welcome to ${req.params.name}!

`);
};

In the two examples above, the first line shows the use of .. to have() 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 answer is .send () The method of the response object. You can enter any text or HTML in parentheses. Access for dynamic routes req.params.name (Because you used it /: first names(From query object) returns the value of dynamic route parameter (from query object)first names in that 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, To put, Or Wipe off 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 perform the callback function provided in. .. Listen() Method.

Express server running

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

Express.js-root root demo

Likewise, when accessing the next dynamic route http: // localhost: 5000 / muoThe second handler function is executed and you should see something like this:

Express.js-Dynamic route demo

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 create a basic web server with Go

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

Read more

About the Author

..


Source link

Leave a Reply

Your email address will not be published. Required fields are marked *