The Basics of Node - Intro to Requests and Responses
If you haven't, you can check my previous post on Creating Node Server. This post continues from creating a Node server
, a post on how to create a simple Node Server.
Requests
are essentially browsers asking for information from the server. For example, when you input a text into Google, you are sending a request. The information you see after the search has occurred is the response
returned to you by the Google server. We would get to the server responses
in a bit.
We already have ourselves a Server, let launch the server;
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req);
});
server.listen(3000);
After going to the localhost:3000
on your browser, go back to your console to check what was logged to the console. There you see a long list of objects, variables, and functions. All these are what constitutes the request which was sent from the browser. Here is a screenshot of a part of the request that was logged into the console
There is so much information contained in the request we have from the browser. For example, we can see the following:
- URL from which the request was made,
- request methods,
- the HTTP version number of the request,
- browser's cookies,
- information from forms filled in the browser,
- Operating system on which the browser is running,
- type of browser used in making the request and so much more.
We will only be using few parts if this information we have received. For starters, One can check the request URL. Instead of logging the whole request object, let's log only the URL;
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req.url);
});
server.listen(3000);
After restarting the server and refreshing the page on the browser, we have '/'
logged into the console. You can modify the URL on your browser to, say localhost:3000/user, and rerun the server, you get '/user'
logged as the URL into the console. You can also check for the method
type of the request.
This information we receive as the request is what we use to determine the type of response the browser gets.
Now, Let's get to server responses
.
Responses are the information sent back to the browsers. A response can take various forms, it could be a text, file like HTML, JSON, XML, Image. Now how do we send responses to the browser? As the req
is used for requests likewise the res
is used for responses.
The res
object has methods that we would use to send data to the browser. Let's take a look at the following code.
const server = http.createServer((req, res) => {
res.setHeader('Content-Type': 'text/html');
});
The set header method receives a Content-Type of text/html. This response is telling the browser that the type of content it will receive is HTML. Note: that there is only a set of limited headers that browsers recognize. We would learn about a package that helps us set the Content-Type automatically for us in the coming posts. Now let's add the HTML code that we are sending to the browser.
const http = require('http');
const server = http.createServer((req, res) => {
res.setHeader('Content-Type': 'text/html');
res.write('<html>');
res.write(`<head><title>Receiver</title></head>`);
res.write(`<body>
<h1>Welcome to my Message Receiver</h1>
<p>I am the message from Earth realm-41</p>
</body>`);
res.write('</html>');
res.end();
});
server.listen(3000);
Remember to include res.end()
to denote the end of the response you sent. NOTE: There should not be any sending of new response data declaring res.end()
. It will lead to an error.
If you restart the server and refresh the page of your browser, you should see that the page now contains the HTML code we have sent as the response to the browser.
This is so cool, we have created our Node Server, looked into the request, and sent a response after the browser has sent a request. Next, we will look into dealing with different request routes and sending different responses according to the different routes.