Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

10. Pro HTML5 Application Hacks with Nod... > Hack 81. Use Node.js’s Response Obje...

Hack 81. Use Node.js’s Response Object to Respond to the Client with Device-Specific Data

You can use the response object in Node.js to display client-specific information in the browser.

Now that we have hacked our request object, let’s tackle the response. We can begin by calling the writeHead() method and passing the success response code, along with the Content-Type set to text/plain. This is a pretty standard response. Then we will call the end() method and pass it a specific string based on the properties we added to our global DeviceData object.

res.writeHead(200, {'Content-Type': 'text/plain'});

  if (DeviceData.mobile) {
    res.end('Hello Html5\n Request from a Mobile Device');
  }

  if (DeviceData

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint