You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.3 KiB

8 years ago
  1. # vary
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Manipulate the HTTP Vary header
  8. ## Installation
  9. ```sh
  10. $ npm install vary
  11. ```
  12. ## API
  13. ```js
  14. var vary = require('vary')
  15. ```
  16. ### vary(res, field)
  17. Adds the given header `field` to the `Vary` response header of `res`.
  18. This can be a string of a single field, a string of a valid `Vary`
  19. header, or an array of multiple fields.
  20. This will append the header if not already listed, otherwise leaves
  21. it listed in the current location.
  22. ```js
  23. // Append "Origin" to the Vary header of the response
  24. vary(res, 'Origin')
  25. ```
  26. ### vary.append(header, field)
  27. Adds the given header `field` to the `Vary` response header string `header`.
  28. This can be a string of a single field, a string of a valid `Vary` header,
  29. or an array of multiple fields.
  30. This will append the header if not already listed, otherwise leaves
  31. it listed in the current location. The new header string is returned.
  32. ```js
  33. // Get header string appending "Origin" to "Accept, User-Agent"
  34. vary.append('Accept, User-Agent', 'Origin')
  35. ```
  36. ## Examples
  37. ### Updating the Vary header when content is based on it
  38. ```js
  39. var http = require('http')
  40. var vary = require('vary')
  41. http.createServer(function onRequest(req, res) {
  42. // about to user-agent sniff
  43. vary(res, 'User-Agent')
  44. var ua = req.headers['user-agent'] || ''
  45. var isMobile = /mobi|android|touch|mini/i.test(ua)
  46. // serve site, depending on isMobile
  47. res.setHeader('Content-Type', 'text/html')
  48. res.end('You are (probably) ' + (isMobile ? '' : 'not ') + 'a mobile user')
  49. })
  50. ```
  51. ## Testing
  52. ```sh
  53. $ npm test
  54. ```
  55. ## License
  56. [MIT](LICENSE)
  57. [npm-image]: https://img.shields.io/npm/v/vary.svg
  58. [npm-url]: https://npmjs.org/package/vary
  59. [node-version-image]: https://img.shields.io/node/v/vary.svg
  60. [node-version-url]: http://nodejs.org/download/
  61. [travis-image]: https://img.shields.io/travis/jshttp/vary/master.svg
  62. [travis-url]: https://travis-ci.org/jshttp/vary
  63. [coveralls-image]: https://img.shields.io/coveralls/jshttp/vary/master.svg
  64. [coveralls-url]: https://coveralls.io/r/jshttp/vary
  65. [downloads-image]: https://img.shields.io/npm/dm/vary.svg
  66. [downloads-url]: https://npmjs.org/package/vary