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 80. Detect the User Agent Strin...

Hack 80. Detect the User Agent String Within the Request Object

You can interrogate the HTTP request object to find valuable information about the client user agent.

As you can see, quite a few properties are included in Node.js’s request and response objects. In [Hack #79] we explored a few of them. Here, we will focus on the request headers and extract the user agent string in order to determine the type of client device that originated the request.

User agent sniffing is a common practice, especially with the recent explosion in the use of the mobile web and the fragmentation of mobile web browsers. So this is a very relevant hack when building HTML5 web applications.

Sometimes it is necessary for application developers to know the type of client device that is making a request for data. If the data is available after parsing the user agent headers, the application can then query a lookup web service that contains detailed properties about the particular device. Properties such as screen size, camera access, and hardware acceleration, just to name a few, can then be used to build conditional logic around which assets and markup are returned to the client.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free 10-Day Trial


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