Implementing Dynamic GET Servers

You will use Node.js webservers to serve dynamic content more often than you’ll use them to serve static content. This content may be dynamic HTML files or snippets, JSON data, or a number of other types of data. To serve a GET request dynamically, you need to implement code in the request handler that dynamically populates the data you want to send back to the client, write it out to the response, and then call end() to finalize the response and flush the Writable stream.

Listing 7.3 shows the basic implementation of a dynamic webserver. In this case, the webserver simply responds with a dynamically generated HTTP file. The example is designed to show the process of sending the headers, building the response, and then sending the data in a series of write() requests.

Notice that line 6 creates the server, using createServer(), and line 15 begins listening on port 8080, using listen(). Inside the request event handler defined in lines 7–15, the Content-Type header is set, and then the headers are sent, with a response code of 200. In reality, you would have already done a lot of processing to prepare the data. But in this case, the data is just the messages array defined in lines 2–5.

Notice that in lines 11–13, the loop iterates through the messages and calls write() each time to stream the response to the client. Then in line 14, the response is completed with a call to end().

Listing 7.3 http_server_get.js: Implementing a basic GET webserver


01 var http = require('http'),
02 var messages = [
03   'Hello World',
04   'From a basic Node.js server',
05   'Take Luck'];
06 http.createServer(function (req, res) {
07   res.setHeader("Content-Type", "text/html");
08   res.writeHead(200);
09   res.write('<html><head><title>Simple HTTP Server</title></head>'),
10   res.write('<body>'),
11   for (var idx in  messages){
12     res.write(' <h1>' + messages[idx] + '</h1>'),
13   }
14   res.end(' </body></html>'),
15 }).listen(8080);


Listing 7.4 shows a basic implementation of an HTTP client that reads the response from the server in Listing 7.3. This is very similar to the example in Listing 7.2, but note that no path was specified because the service doesn’t really require one. For more complex services, you would implement query strings or complex path routes to handle a variety of calls.

Note that on line 11 the statusCode from the response is logged to the console. Also, on line 12 the headers from the response are also logged. Then on line 13 the full response from the server is logged. Figure 7.3 shows the output of the HTTP client as well as the dynamic GET server being accessed from a web browser.

Listing 7.4 http_client_get.js: A basic web client making a GET request to the server in Listing 7.3


01 var http = require('http'),
02 var options = {
03     hostname: 'localhost',
04     port: '8080',
05   };
06 function handleResponse(response) {
07   var serverData = '';
08   response.on('data', function (chunk) {
09     serverData += chunk;
10   });
11   response.on('end', function () {
12     console.log("Response Status:", response.statusCode);
13     console.log("Response Headers:", response.headers);
14     console.log(serverData);
15   });
16 }
17 http.request(options, function(response){
18   handleResponse(response);
19 }).end();


Image

Figure 7.3 Implementing and accessing a basic HTTP GET server.

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

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