GithubHelp home page GithubHelp logo

mangrove / negotiator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jshttp/negotiator

0.0 6.0 0.0 115 KB

An HTTP content negotiator for node.js written in javascript.

License: Other

JavaScript 100.00%

negotiator's Introduction

Negotiator

An HTTP content negotiator for node.js written in javascript.

Accept Negotiation

Negotiator = require('negotiator')

availableMediaTypes = ['text/html', 'text/plain', 'application/json']

// The negotiator constructor receives a request object
negotiator = new Negotiator(request)

// Let's say Accept header is 'text/html, application/*;q=0.2, image/jpeg';q=0.8

negotiator.preferredMediaTypes()
// -> ['text/html', 'application/*', 'image/jpeg']

negotiator.preferredMediaTypes(availableMediaTypes)
// -> ['text/html', 'application.json']

negotiator.preferredLanguage(availableMediaTypes)
// -> 'text/html'

You can check a working example at examples/accept.js.

Methods

preferredMediaTypes(availableMediaTypes):

Returns an array of preferred media types ordered by priority from a list of available media types.

preferredMediaType(availableMediaType):

Returns the top preferred media type from a list of available media types.

Accept-Language Negotiation

Negotiator = require('negotiator')

negotiator = new Negotiator(request)

availableLanguages = 'en', 'es', 'fr'

// Let's say Accept-Language header is 'en;q=0.8, es, pt'

negotiator.preferredLanguages()
// -> ['es', 'pt', 'en']

negotiator.preferredLanguages(availableLanguages)
// -> ['es', 'en']

language = negotiator.preferredLanguage(availableLanguages)
// -> 'es'

You can check a working example at examples/language.js.

Methods

preferredLanguages(availableLanguages):

Returns an array of preferred languages ordered by priority from a list of available languages.

preferredLanguage(availableLanguages):

Returns the top preferred language from a list of available languages.

Accept-Charset Negotiation

Negotiator = require('negotiator')

availableCharsets = ['utf-8', 'iso-8859-1', 'iso-8859-5']

negotiator = new Negotiator(request)

// Let's say Accept-Charset header is 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2'

negotiator.preferredCharsets()
// -> ['utf-8', 'iso-8859-1', 'utf-7']

negotiator.preferredCharsets(availableCharsets)
// -> ['utf-8', 'iso-8859-1']

negotiator.preferredCharset(availableCharsets)
// -> 'utf-8'

You can check a working example at examples/charset.js.

Methods

preferredCharsets(availableCharsets):

Returns an array of preferred charsets ordered by priority from a list of available charsets.

preferredCharset(availableCharsets):

Returns the top preferred charset from a list of available charsets.

Accept-Encoding Negotiation

Negotiator = require('negotiator').Negotiator

availableEncodings = ['identity', 'gzip']

negotiator = new Negotiator(request)

// Let's say Accept-Encoding header is 'gzip, compress;q=0.2, identity;q=0.5'

negotiator.preferredEncodings()
// -> ['gzip', 'identity', 'compress']

negotiator.preferredEncodings(availableEncodings)
// -> ['gzip', 'identity']

negotiator.preferredEncoding(availableEncodings)
// -> 'gzip'

You can check a working example at examples/encoding.js.

Methods

preferredEncodings(availableEncodings):

Returns an array of preferred encodings ordered by priority from a list of available encodings.

preferredEncoding(availableEncodings):

Returns the top preferred encoding from a list of available encodings.

License

MIT

negotiator's People

Contributors

domenic avatar federomero avatar isaacs avatar shockie avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  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.