My First Week as a CEO

From the moment I had first dreamed of building a company, I had never visualized myself with the capacity to do what I am able to do today. And more of what I am doing today looks like doing less in…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to create a Web Server with Nodejs HTTP and FS modules

Today, I will talk about creating a web server with NodeJs.

Basically, Nodejs is a js working environment that works asynchronously contrary to js.

I will create a web server and log responses to JSON file today. I will use HTTP module for creating a web server and the fs module for logging responses to file. HTTP and fs are NodeJs core modules so you don’t need to install any third-party dependencies.

The web server includes several parts that control how web users access hosted files. An HTTP server is a software that understands URLs (web addresses) and HTTP (the protocol your browser uses to view webpages). An HTTP server can be accessed through the domain names of the websites it stores, and it delivers the content of these hosted websites to the end user’s device.

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server.

To make the request, the client uses components of a URL (Uniform Resource Locator), which includes the information needed to access the resource.

After getting basic information about web servers and HTTP requests let’s start to code.

We are importing our HTTP module and creating our server by the createServer method. Our createServer method takes two params as request and response. We can reach the requested URL with the request.url. After that, we can create a switch case block or if-else statement for our requested URLs and create a response to these requests.

We can get the current time with the codes below

Then we can add the fs module for logging requests to our JSON file. We can use different methods for this. fs.appendFile will create a new file if it is not created before and append our logs to it but fs.writeFile will create a new file if it is not created before and do this process for every request as a default so our older logs will be deleted. We can use the {flag: “a”} option for solving this issue.

I also used the js date object for reaching the current time when the request is done. Created JSON file and inserted request URL, status, and current time to it.

You can also use different packages for saving logs like Winston, Bunyan, Pino, Morgan, Node-loggly.

There are many ways to create a web server with NodeJs. You can use expressJs for creating a web server and a moment package for getting the current time. These packages will ease the process but we tried to use core modules today. I hope you liked it. See you in the next article…

Add a comment

Related posts:

Are You Stressing Yourself Out? Your Hair Knows

Stress is known to cause hair loss, but a new study indicates that scientists can determine the stress an individual is experiencing by analyzing the lock of hair. In the past, professionals would…

Does quality early education lead to more successful lives as adults?

Education broadens one’s perspective and horizon. It cultivates a way of seeing life in us, and also, it’s the most crucial aspect of the country’s and an individual’s development. We learn about the…

Introduction to PySpark

The Apache Spark is a fast and powerful framework that provides an API to perform massive distributed processing over resilient sets of data. It also ensures data processing with lightning speed and…