Driven by a passion for innovation and a relentless pursuit of excellence, I am excited about the challenges and opportunities that the world of full-stack development presents. Lets GooooπͺπΌπ
Fundamentals of Node JS
Creating routes in Node JS
Making a server request
Whenever we are making a request to the server, we have a parameter called request or req as we used in our code base ππΌ
And on the req parameter, we have a property called URL. The URL property stores the value which the user has entered after the root URL.
Update the code base ππΌ
Go to the browser, type in the root URL and the resource. i.e localhost:8000/home
to see the outcome on the browser.
Don't forget to restart your server.
See from the above image we got the response of the value home
which the user entered as a parameter after the root URL. This is how we get the value which the user typed after the root URL and based on that value we can return different responses. Lets write some logic to navigate the web page using some dynamic resource.
We are handling four routes: The root URL "/"
, the home "/home"
, about "/about"
and contact page "/contact"
respectively. ππΌ
we get a dynamic response as we use each dynamic value as URL.
For the home page i.e localhost:8000/home
ππΌ
For the about page i.e localhost:8000/about
ππΌ
For the contact page i.e localhost:8000/contact
ππΌ
What if the user enters a URL that was not handled using our routes? i.e localhost:8000/support
. Well if this happens to be the case then we won't get any response from the browser and also the page will be on infinite loading. ππΌ
This is happening because in our node application we are not handling the "/support"
URL and the server is clueless on what response to send. To avoid all of this we need to specify a fallback route. This route will be called when none of the other routes can handle the URL which the user has entered in the address bar. So we need specify a default path.
From the image below you can see from the last if/else
block how we handled the non-existing route. ππΌ
And now when a user enters a non-existing route to the address bar, ππΌ
The server sends an error response.
Always remember to restart the server to update the browser βΌοΈ
Setting response headers
We use the writeHead()
method to set response headers in Node JS. Always remember to set the header
and statusCode
before sending the response. The writeHead()
method receives the statusCode
as an argument. i.e res.writeHead(200)
. The parameter 200 is the statusCode
. Lets update our code base by adding the headers. ππΌ
In the code base using the image above as a reference, we used statusCode
404 for the default response which means resource not found. The statusCode
200 signifies a success.
To see the statusCode
of our web page, open the developer tools. ππΌ
Go to the network tab ππΌ
Restart the sever and refresh the page to see the network statusCode
ππΌ
We have a statusCode
of 200 because its a valid route.
Lets try an invalid route to see the network statusCode
ππΌ
The header
of a response contains some additional information about the response. For example, the type of response we are sending to the client and the time the response was sent. To define a header for a response, we pass an object as a second argument to the writeHead()
method. Lets update our code with the header object. ππΌ
The header
object has a key value pair with "Content-Type"
as the key and "text"
as the value because we are only passing a text to the browser.
Lets inspect the web page and see the update on the network tab. ππΌ
Next: Working with JSON data
See you soonππ«‘βπΌ...