Node.js 101 - part 2: First web server
Tags: javascript, node, beginners, tutorial
Published: 2021-03-10 18:22:45

---
title: Node.js 101 - part 2: First web server
published: true
description: 
tags: javascript, node, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4anecy5mdl4pho8w7519.jpg
series: Node.js 101
---

I strongly recommend learning javascript first. Here a series of post I did on Dev.to: https://dev.to/rickavmaniac/javascript-my-learning-journey-part-1-what-is-javascript-role-and-use-case-49a3

I am now ready to continue my Node.js learning journey :)

Click follow if you want to miss nothing. I will publish here on Dev.to what I learn everyday. 

Without further ado here is a summary of my notes for my last day.

<h2>Create my first Web Server</h2>
```js
// the http module have method to help create the server
const http = require('http')

// Create the server instance
// req :  incoming request
// res :  outgoing response

const server = http.createServer((req, res) => {
    // send a response to client
    res.end('Hello World from the server')
})

// start server listening for request
server.listen(5000, 'localhost', () => {
    console.log('Server is listening at localhost on port 5000')
})
```
![First Server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lcccyoblnk1k8x8s4avi.png)

<h2>Basic Routing</h2>
This server is great but currently he do not react to different url we could send from the browser. If we send for example http://localhost:5000/friends it show the exact same response as http://localhost:5000. Let implement that specific behaviour call routing.

With the server instance we just create, the req variable contain the incoming request informations.

This incoming request have a property call url that return current page path we currently visit.

For example if we visit the friends page at localhost:5000/friends the req.url property will return '/friends'.

If we visit the root path localhost:5000, the req.url will contain only empty '/'

Here a example very basic routing
```js
const http = require('http')

// Create the server instance
// req :  incoming request
// res :  outgoing response
const server = http.createServer((req, res) => {
    const pathName = req.url
    if (pathName === '/friends') {
        res.end('This is my friends page')
    } else if (pathName === '/') {
        res.end('Hello World from the server root')
    }
})

// start server listening for request
server.listen(5000, 'localhost', () => {
    console.log('Server is listening at localhost on port 5000')
})
```
Noted if you visit a page not include in the if/else routing the server will continue to loop indefinitely. So best thing to do is to add a else statement for all other non manage route.
```js
if (pathName === '/friends') {
    res.end('This is my friends page')
} else if (pathName === '/') {
    res.end('Hello World from the server root')
} else {
    res.end('Page not found')
}
```

To make that even more pro, we can add a 404 header to our response. Since we write a header we will also add a Content-type to 'text/html' that will allow us to use HTML in the response. 

```js
 if (pathName === '/friends') {
     res.end('This is my friends page')
 } else if (pathName === '/') {
     res.end('Hello World from the server root')
 } else {
     res.writeHead(404, {
         'Content-type': 'text/html'
     })
     res.end('<h1>Page not found</h1>')
 }
```
![Page 404](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ze06hw3btqd3xik7pok6.png)

<h2>Conclusion</h2>
That's it for today. Tomorrow will put all that learning to good use and will make our first API. Stay tune!

Follow me on Twitter: <a href="https://twitter.com/justericchapman?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow @justericchapman</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>