Nodejs: cd my-journey01
Tags: javascript, programming, node, devjournal
Published: 2022-05-07 22:31:33

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

	

## Anatomy of a nodejs server 

```javascript 

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);

```

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

```javascript 
var http = require('http');
```

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

```javascript
var url = require('url');
```

---

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). 

```javascript 
http.createServer(function (){
//Statements go here 
}).listen()
```

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:
```javascript 
object = { property:value }
``` 
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. 
```javascript

function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
```
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_~~
```javascript 
var qr = url.parse(req.url, true).query;
```
Take for example when set to true 

![True querystring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/elj9zqofykdmnffvt7u6.png)

and 

When set to false
![False querystring](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6ncyrlienbuf7dm67rx.png)

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).
```javascript 
var text = qr.year + "" + qr.month;
res.end(txt);
```
Hence when we access, query  year and month
![Query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n06hfltao52wfg6brswm.png)

We get the result as

![QueryResult](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stt2nbvizi9d983kjbg3.png)

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](W3Schools.com), [Geeksforgeeks](geeksforgeeks.org), Google.