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 :)
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 serverconst http = require('http')// Create the server instance// req : incoming request// res : outgoing responseconst server = http.createServer((req, res) => { // send a response to client res.end('Hello World from the server')})// start server listening for requestserver.listen(5000, 'localhost', () => { console.log('Server is listening at localhost on port 5000')})
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 responseconst 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 requestserver.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>') }
Conclusion
That's it for today. Tomorrow will put all that learning to good use and will make our first API. Stay tune!
Original Link: https://dev.to/ericchapman/node-js-101-part-2-first-web-server-5gl8
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To