Giter Site home page Giter Site logo

accepts's Introduction

accepts

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Higher level content negotiation based on negotiator. Extracted from koa for general use.

In addition to negotiator, it allows:

  • Allows types as an array or arguments list, ie (['text/html', 'application/json']) as well as ('text/html', 'application/json').
  • Allows type shorthands such as json.
  • Returns false when no types match
  • Treats non-existent headers as *

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install accepts

API

var accepts = require('accepts')

accepts(req)

Create a new Accepts object for the given req.

.charset(charsets)

Return the first accepted charset. If nothing in charsets is accepted, then false is returned.

.charsets()

Return the charsets that the request accepts, in the order of the client's preference (most preferred first).

.encoding(encodings)

Return the first accepted encoding. If nothing in encodings is accepted, then false is returned.

.encodings()

Return the encodings that the request accepts, in the order of the client's preference (most preferred first).

.language(languages)

Return the first accepted language. If nothing in languages is accepted, then false is returned.

.languages()

Return the languages that the request accepts, in the order of the client's preference (most preferred first).

.type(types)

Return the first accepted type (and it is returned as the same text as what appears in the types array). If nothing in types is accepted, then false is returned.

The types array can contain full MIME types or file extensions. Any value that is not a full MIME types is passed to require('mime-types').lookup.

.types()

Return the types that the request accepts, in the order of the client's preference (most preferred first).

Examples

Simple type negotiation

This simple example shows how to use accepts to return a different typed respond body based on what the client wants to accept. The server lists it's preferences in order and will get back the best match between the client and server.

var accepts = require('accepts')
var http = require('http')

function app (req, res) {
  var accept = accepts(req)

  // the order of this list is significant; should be server preferred order
  switch (accept.type(['json', 'html'])) {
    case 'json':
      res.setHeader('Content-Type', 'application/json')
      res.write('{"hello":"world!"}')
      break
    case 'html':
      res.setHeader('Content-Type', 'text/html')
      res.write('<b>hello, world!</b>')
      break
    default:
      // the fallback is text/plain, so no need to specify it above
      res.setHeader('Content-Type', 'text/plain')
      res.write('hello, world!')
      break
  }

  res.end()
}

http.createServer(app).listen(3000)

You can test this out with the cURL program:

curl -I -H'Accept: text/html' http://localhost:3000/

License

MIT

accepts's People

Contributors

dougwilson avatar federomero avatar hayeah avatar jonathanong avatar nook-scheel avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.