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.

101 lines
2.7 KiB

4 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. This is a [Node.js](https://nodejs.org/en/) module available through the
  10. [npm registry](https://www.npmjs.com/). Installation is done using the
  11. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  12. ```sh
  13. $ npm install vary
  14. ```
  15. ## API
  16. <!-- eslint-disable no-unused-vars -->
  17. ```js
  18. var vary = require('vary')
  19. ```
  20. ### vary(res, field)
  21. Adds the given header `field` to the `Vary` response header of `res`.
  22. This can be a string of a single field, a string of a valid `Vary`
  23. header, or an array of multiple fields.
  24. This will append the header if not already listed, otherwise leaves
  25. it listed in the current location.
  26. <!-- eslint-disable no-undef -->
  27. ```js
  28. // Append "Origin" to the Vary header of the response
  29. vary(res, 'Origin')
  30. ```
  31. ### vary.append(header, field)
  32. Adds the given header `field` to the `Vary` response header string `header`.
  33. This can be a string of a single field, a string of a valid `Vary` header,
  34. or an array of multiple fields.
  35. This will append the header if not already listed, otherwise leaves
  36. it listed in the current location. The new header string is returned.
  37. <!-- eslint-disable no-undef -->
  38. ```js
  39. // Get header string appending "Origin" to "Accept, User-Agent"
  40. vary.append('Accept, User-Agent', 'Origin')
  41. ```
  42. ## Examples
  43. ### Updating the Vary header when content is based on it
  44. ```js
  45. var http = require('http')
  46. var vary = require('vary')
  47. http.createServer(function onRequest (req, res) {
  48. // about to user-agent sniff
  49. vary(res, 'User-Agent')
  50. var ua = req.headers['user-agent'] || ''
  51. var isMobile = /mobi|android|touch|mini/i.test(ua)
  52. // serve site, depending on isMobile
  53. res.setHeader('Content-Type', 'text/html')
  54. res.end('You are (probably) ' + (isMobile ? '' : 'not ') + 'a mobile user')
  55. })
  56. ```
  57. ## Testing
  58. ```sh
  59. $ npm test
  60. ```
  61. ## License
  62. [MIT](LICENSE)
  63. [npm-image]: https://img.shields.io/npm/v/vary.svg
  64. [npm-url]: https://npmjs.org/package/vary
  65. [node-version-image]: https://img.shields.io/node/v/vary.svg
  66. [node-version-url]: https://nodejs.org/en/download
  67. [travis-image]: https://img.shields.io/travis/jshttp/vary/master.svg
  68. [travis-url]: https://travis-ci.org/jshttp/vary
  69. [coveralls-image]: https://img.shields.io/coveralls/jshttp/vary/master.svg
  70. [coveralls-url]: https://coveralls.io/r/jshttp/vary
  71. [downloads-image]: https://img.shields.io/npm/dm/vary.svg
  72. [downloads-url]: https://npmjs.org/package/vary