Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
forked fromjshttp/accepts

Higher-level content negotiation

License

NotificationsYou must be signed in to change notification settings

Morin3/accepts

 
 

Repository files navigation

NPM VersionNPM DownloadsNode.js VersionBuild StatusTest Coverage

Higher level content negotiation based onnegotiator.Extracted fromkoa 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 asjson.
  • Returnsfalse when no types match
  • Treats non-existent headers as*

Installation

This is aNode.js module available through thenpm registry. Installation is done using thenpm install command:

$ npm install accepts

API

varaccepts=require('accepts')

accepts(req)

Create a newAccepts object for the givenreq.

.charset(charsets)

Return the first accepted charset. If nothing incharsets is accepted,thenfalse is returned.

.charsets()

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

.encoding(encodings)

Return the first accepted encoding. If nothing inencodings is accepted,thenfalse is returned.

.encodings()

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

.language(languages)

Return the first accepted language. If nothing inlanguages is accepted,thenfalse is returned.

.languages()

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

.type(types)

Return the first accepted type (and it is returned as the same text as whatappears in thetypes array). If nothing intypes is accepted, thenfalseis returned.

Thetypes array can contain full MIME types or file extensions. Any valuethat is not a full MIME type is passed torequire('mime-types').lookup.

.types()

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

Examples

Simple type negotiation

This simple example shows how to useaccepts to return a different typedrespond body based on what the client wants to accept. The server lists it'spreferences in order and will get back the best match between the client andserver.

varaccepts=require('accepts')varhttp=require('http')functionapp(req,res){varaccept=accepts(req)// the order of this list is significant; should be server preferred orderswitch(accept.type(['json','html'])){case'json':res.setHeader('Content-Type','application/json')res.write('{"hello":"world!"}')breakcase'html':res.setHeader('Content-Type','text/html')res.write('<b>hello, world!</b>')breakdefault:// the fallback is text/plain, so no need to specify it aboveres.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

About

Higher-level content negotiation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript100.0%

[8]ページ先頭

©2009-2025 Movatter.jp