DEV Community

Cover image for Nodejs: cd my-journey01
FFFF:0000h
FFFF:0000h

Posted on

Nodejs: cd my-journey01

Today I learned about built-in modules in Node.js such as the HTTP module and the URL module.

Anatomy of a nodejs server


var http = require('http');
var url = require('url');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  var qr = url.parse(req.url, true).query;
  var text = qr.year + " " + qr.month;
  res.end(txt);
}).listen(8080);

Enter fullscreen mode Exit fullscreen mode

The require() function is used to include the HTTP module and is stored in the variable, http to be used.

var http = require('http');
Enter fullscreen mode Exit fullscreen mode

The require() function is used to include the URL module and is stored in the variable, url to be used.

var url = require('url');
Enter fullscreen mode Exit fullscreen mode

Now the HTTP module allows Nodejs to transfer data over the protocol using methods like createServer() and listen() method which allows Nodejs function as a Web server (an HTTP server)

Whereas the URL module allows Nodejs split query string into readable parts.


The http variable object which has the HTTP module stored in it has a method createServer which takes in a function parameter,and this function is called when anyone tries to access the port 8080. It's important to know that that the createServer actually listens to the server port incase there's any access and loads up the function passed in as an argument and sends response back to the client (the Web browser).

http.createServer(function (){
//Statements go here 
}).listen()
Enter fullscreen mode Exit fullscreen mode

Analysing the content of the function parameter. I got to find out interesting things.

The function, which is a nameless function takes in two arguments req (request) and res (response).

P.O.V : Server.
The req argument which is an "http.IncomingMessage object (containers for named values)" represents the request from the client to the server.

The res argument which is an "http.outgoingmessage object" represents the response from the server to the client.
The res.writeHead function or the res object which contains a named value writeHead which is a function which is a method (because methods are functions that are in objects) and is used to write HTTP headers (HTTP headers let the client and the server pass additional information with an HTTP request or response.)
At this point I was glad I understood the concept of objects and methods and functions in JavaScript.

The writeHead() method takes in two arguments the status code which is 200 (meaning successful connection, sent by the server to the client) and the type of content to be sent to the client (web browser), in this case an html content type.
Remembering that the res is an object and objects are containers for named values, and objects are written in such manner:

object = { property:value }
Enter fullscreen mode Exit fullscreen mode

I quickly spot that the 'Content-Type':'text/html' part is a member of an object but wait, it's within the writeHead function/method so it must belong to the writeHead, it all began to make sense, "a server is like a nest of objects, functions and objects", I said to myself.


function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
Enter fullscreen mode Exit fullscreen mode

Now below, there's the url.parse method which takes a url address (req.url) and parses it returning a url object with each part of the address, stored in the qr object variable. The second parameter true is called a parseQueryString and it is a boolean value. If it is set to true then the query property will be set to an object returned by the querystring module’s parse() method. If it's set to false then the query property on the returned URL object will be an unparsed, undecoded string. Its default value is false.
The .query at the end is so parts can be splitted when we query

var qr = url.parse(req.url, true).query;
Enter fullscreen mode Exit fullscreen mode

Take for example when set to true

True querystring

and

When set to false
False querystring

So, we have a variable text which will take in the part of our url named year and month and prints them out as html text in our specified port because we have instructed our server to end the response (res.end) with it (text).

var text = qr.year + "" + qr.month;
res.end(txt);
Enter fullscreen mode Exit fullscreen mode

Hence when we access, query year and month
Query

We get the result as

QueryResult

This is basically how websites work when we want to access a content from n-month in n-year and if there's any data available, it displays or shows an error, sent from the server.

Things learned: A lot (lol), objects, functions, methods, modules, Nodejs, server, backend.

Cover-image: Author
Resources:W3Schools, Geeksforgeeks, Google.

Top comments (0)