This is a
playground to test code. It runs a full
Node.js environment and already has all of
npm’s 400,000 packages pre-installed, including
connect-conneg with all
npm packages installed. Try it out:
This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.
A module meant to be plugged into the NodeJS connect middleware, which processes the Accept, Accept-Languages, and Accept-Charset HTTP headers to enable you to prioritize the type of data returned down the wire for an HTTP request.
This will modify the Request object, adding a list of strings which are sorted based on the qvalue sent in the HTTP header. These will be mapped to the request object as follows:
These are sorted per the rules in RFC 2616, Section 14.
Consider the following HTTP header from my browser:
Accept-Languages: en-US; q=0.8, en; q=0.5, es
This header indicates the user wants to recieve US English, any English, or any Spanish. Since a q-value is not assigned to Spanish, it will be assigned the q-value 1, per the HTTP/1.1 spec. When sorted on q-value we get the following result in request.languages
['es', 'en-US', 'en']
When preparing to render the response to the client, you can reference this list, shifting results off the front of the list until you encounter a language code you can honor, and returning content customized to your users language of choice. If they do not request a language you can honor, then you can choose to return a default language or an error.
Future versions of this library will include helper methods to perform the lookups against these arrays.