DEV Community

Cover image for Node.js 101 - part 2: First web server
Eric The Coder
Eric The Coder

Posted on • Edited on

Node.js 101 - part 2: First web server

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.

Create my first Web Server

// 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')
})
Enter fullscreen mode Exit fullscreen mode

First Server

Basic Routing

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

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')
})
Enter fullscreen mode Exit fullscreen mode

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.

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')
}
Enter fullscreen mode Exit fullscreen mode

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.

 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>')
 }
Enter fullscreen mode Exit fullscreen mode

Page 404

Conclusion

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: Follow @justericchapman

Top comments (1)

Collapse
 
joelnwalkley profile image
Joel N. Walkley

Neat post - I haven't considered doing direct routing before as I'm also learning Node. I just went straight for Express to help me out with the api writing.