http-accept
Connect compatible middleware that parses HTTP Accept header fields
Last updated 6 years ago by niclashoyer .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install http-accept 
SYNC missed versions from official npm registry.

Accept

Accept parses all accept header fields and sorts them according to the HTTP specification.

Supported Header Fields

Accept

Parsed into req.accept.types.

Accept-Charset

Parsed into req.accept.charsets.

Accept-Encoding

Parsed into req.accept.encodings.

Accept-Language

Parsed into req.accept.languages.

Accept-Ranges

Parsed into req.accept.ranges.

Installation

npm install http-accept

Usage

Just require 'http-accept' and throw it into a connect compatible middlware stack.

connect  = require 'connect'
http     = require 'http'
accept   = require 'http-accept'

app = connect()
app.use connect.logger 'dev'
app.use accept
app.use (req, res) ->
	console.log req.accept
	res.end()

app.listen 3000

A request from a browser on http://localhost:3000 would print out

{ types: 
   [ { type: 'text',
       subtype: 'html',
       params: {},
       mediarange: 'text/html',
       quality: 1 },
     { type: 'application',
       subtype: 'xhtml+xml',
       params: {},
       mediarange: 'application/xhtml+xml',
       quality: 1 },
     { type: 'application',
       subtype: 'xml',
       params: [Object],
       mediarange: 'application/xml',
       quality: 0.9 },
     { type: '*',
       subtype: '*',
       params: [Object],
       mediarange: '*/*',
       quality: 0.8 } ],
  charsets: undefined,
  encodings: [ 'gzip', 'deflate' ],
  languages: [ 'de-de', 'de', 'en-us', 'en' ],
  ranges: undefined }

For each header present in the request, there is also a getBestMatch method which will find the highest quality match amongst the supplied candidates.

encoding = (req.accept.encodings?.getBestMatch ["gzip", "deflate"]) ? "identity"
mediaType = req.accept.types?.getBestMatch ["text/html", "application/json" ,"application/xml"]

Current Tags

  • 0.1.6                                ...           latest (6 years ago)

9 Versions

  • 0.1.6                                ...           6 years ago
  • 0.1.5                                ...           6 years ago
  • 0.1.4                                ...           6 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.1-3                                ...           8 years ago
  • 0.1.1-2                                ...           8 years ago
  • 0.1.1-1                                ...           8 years ago
  • 0.1.0-2                                ...           8 years ago
  • 0.1.0                                ...           8 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 30
Dependencies (0)
None
Dev Dependencies (0)
None

Copyright 2014 - 2017 © taobao.org |