Day 3 of 100days of code.

Day 3 of 100days of code.

Frontend developer embarking on a dynamic journey into backend development with an aspiration to evolve into a proficient MERN stack DeveloperπŸ˜‰

Β·

4 min read

Fundamentals of Node JS

Creating a simple web server

Import the http node module and assign to a variable: const http = require("http"). In other to build a Node JS web server we need to first create a server and then start the server afterwards. To create the http server : const server = http.createServer()

The createServer() method receives a callback function which will be executed every time a new request hits the server. The callback function receives two parameters: request and response. i.e

const server = http.createServer((req, res) =>{}). Inside the callback function, let's simply log a message πŸ‘‡

The createServer() method is also going to return the server objects.

Listening to server

The next step is to start the server. To start the server we use a method called listen(). It starts the server and listens to new requests. The listen() method takes few parameters. The first parameter is the port number i.e 8000 where the application is running and the second parameter is the localhost. The IP address will be "127.0.0.1". Finally the listen() method takes an optional third parameter which is a callback function which gets executed as the server starts listening to the requests. πŸ‘‡

and we get the below message logged to the console. πŸ‘‡

As you can see from the above image, on the terminal, the server is still running and as long as the server is running, we are able to listen to new requests. To stop the server, use the command: Ctrl C for windows and cmd C for mac. For now lets keep the server running. On your browser, type the URL of your application: 127.0.0.1:8000 and the port number after a column. πŸ‘‡

Getting a message from the server

You get the below message logged to the console as a new request. Every time you visit the URL of your application you get a new request.πŸ‘‡

Getting a response from the server

Instead of logging a message whenever we get a new request we want to send some response. πŸ‘‡

Restart the server and check your browser to see the response using your application URL πŸ‘‡

Routing in Node JS

Routing defines the way in which the client requests are handled by the application endpoint. It defines how an application responds to specific request. It also involves mapping different URLs to specific functions or handlers that will be executed when a request is made to that URL.

There are several types of URL patterns:

  • File based URL

In the URL, we specify the file name we want to access and see in the browser. We type the domain name and the file name we want to access. In the below image, We are mapping the URL with a physical file on the server to access the index.html file. After the request is sent to the server, the server will respond with an index.html file content to the client and the file will be rendered in the browser.πŸ‘‡

  • Resource based URL

In the resource based URL, we eliminate the need of mapping the URL with a physical file. We map a URL pattern to a request handler. In this case, after the domain name we have a slash and after that we are specify a resource. πŸ‘‡

And based on the resource we specify after the root URL, we'll get a response. To handle the request in the backend application we'll have to create a request handler. The request handler can be a function or a file which is going to handle the request and respond accordingly. We can make our application to respond to different URLs using dynamic responses using routing.

Route Parameter

A route or a URL can also receive a parameter. πŸ‘‡

So basically on the above image, after the root URL, we have the resource which is product, and after the resource we have a value. The value 101 is an ID and the ID is a parameter. and base on the parameter which is product ID , we will get the product in the response.

Query Strings

After the resource URL, we specify the query string after a question mark ? symbol. πŸ‘‡

In the above image, after the root URL we have a resource called books and after that we are specifying our question mark ? symbol and after that we specify the key value pairs which are the query strings. in this case author=paks author is the key and paks is the value for the first query string and then we are specifying another query string. To separate two queries we use the & symbol and after that we specify another key value pair id=101 with id as the key and 101 as the value.

So for this URL, the response will get the book whose author is paks with an ID of 101. Using routing, it is possible to extract the query strings from the URL, the parameter from the URL and the resource from the URL as well.

Next: Creating Routes in Node JS

See you soon😊🫑✌🏼...

Β