http module

The final module we are going to take a high-level look at is the http module. This module allows us to create http servers with ease. The following is a simple example of an http server:

import http from 'http';

const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type' : 'application/json'});
res.end(JSON.stringify({here : 'is', some : 'data'}));
});
server.listen(8000, '127.0.0.1');

If we head to localhost:8000 in our browser, we should see the JSON object in our browser. If we wanted to get even fancier, we could send back some basic HTML, such as the following:

const server = https.createServer((req, res) => {
res.writeHead(200, { 'Content-Type' : 'text/html' });
res.end(`
<html>
<head></head>
<body>
<h1>Hello!</h1>
<p>This is from our server!</p>
</body>
</html>
`);
});

Instead of setting our content type to application/json, we set it to text/html so that the browser knows how to interpret this request. Then, we end the response with our basic HTML. How would we be able to respond to the server if our HTML requests a CSS file?

We would need to interpret the request and be able to send some CSS. We could do this with something like the following:

const server = http.createServer((req, res) => {
if( req.method === 'GET' &&
req.url === '/main.css' ) {
res.writeHead(200, { 'Content-Type' : 'text/css' });
res.end(`
h1 {
color : green;
}
p {
color : purple;
}
`);
} else {
res.writeHead(200, { 'Content-Type' : 'text/html' });
// same as above
}
});

We are able to pull various pieces of information from the request that we receive. In this case, all we care about is whether this was a GET request and that it is asking for the main.css resource. If it is, we return the CSS; otherwise, we just return our HTML. It should be noted that this code should look somewhat familiar to web server frameworks such as Express. Express adds a bunch of helper methods and ways to protect our server, but it should be noted that we can write simple servers with fewer dependencies and only by utilizing the modules that are internal to Node.js.

We can also use the http module to fetch data from various resources. If we use the get method built into the http module or even the more generic request method, we can get resources from various other servers. The following code illustrates this:

import https from 'https';

https.get('https://en.wikipedia.org/wiki/Surprise_(emotion)', (res) => {
if( res.statusCode === 200 ) {
res.on('data', (data) => {
console.log(data.toString('utf8'));
});
res.on('end', () => {
console.log('no more information');
});
} else {
console.error('bad error code!', res.statusCode);
}
});

First, we can see that we have to utilize the https module. Since this web page is located on a server that is utilizing Secure Socket Layer (SSL) certificates, we have to use the secure connection method. Then, we simply call the get method, passing in the URL that we want, and read the data from the response. If, for some reason, we do not get a 200 response (an okay message), we error out.

These three modules should showcase that we have quite a bit of power inside of the Node.js ecosystem and should spark some curiosity in how we can use Node.js, without any dependencies, to make useful systems. In the next section, we will take a look at how we can debug our Node.js code in a command-line debugger, along with the code inspection system that we are used to using with Chrome.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.59.192