Node.js 101 – part 2: First web server

I strongly recommend learning javascript frirst. 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 …


This content originally appeared on DEV Community and was authored by Eric Chapman

I strongly recommend learning javascript frirst. 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')
})

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

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.

 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

Conclusion

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


This content originally appeared on DEV Community and was authored by Eric Chapman


Print Share Comment Cite Upload Translate Updates
APA

Eric Chapman | Sciencx (2021-03-10T18:22:45+00:00) Node.js 101 – part 2: First web server. Retrieved from https://www.scien.cx/2021/03/10/node-js-101-part-2-first-web-server/

MLA
" » Node.js 101 – part 2: First web server." Eric Chapman | Sciencx - Wednesday March 10, 2021, https://www.scien.cx/2021/03/10/node-js-101-part-2-first-web-server/
HARVARD
Eric Chapman | Sciencx Wednesday March 10, 2021 » Node.js 101 – part 2: First web server., viewed ,<https://www.scien.cx/2021/03/10/node-js-101-part-2-first-web-server/>
VANCOUVER
Eric Chapman | Sciencx - » Node.js 101 – part 2: First web server. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/10/node-js-101-part-2-first-web-server/
CHICAGO
" » Node.js 101 – part 2: First web server." Eric Chapman | Sciencx - Accessed . https://www.scien.cx/2021/03/10/node-js-101-part-2-first-web-server/
IEEE
" » Node.js 101 – part 2: First web server." Eric Chapman | Sciencx [Online]. Available: https://www.scien.cx/2021/03/10/node-js-101-part-2-first-web-server/. [Accessed: ]
rf:citation
» Node.js 101 – part 2: First web server | Eric Chapman | Sciencx | https://www.scien.cx/2021/03/10/node-js-101-part-2-first-web-server/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.