3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Node.js Version][node-version-image]][node-version-url]
6 [![Build Status][travis-image]][travis-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
9 Manipulate the HTTP Vary header
13 This is a [Node.js](https://nodejs.org/en/) module available through the
14 [npm registry](https://www.npmjs.com/). Installation is done using the
15 [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
23 <!-- eslint-disable no-unused-vars -->
26 var vary = require('vary')
31 Adds the given header `field` to the `Vary` response header of `res`.
32 This can be a string of a single field, a string of a valid `Vary`
33 header, 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.
38 <!-- eslint-disable no-undef -->
41 // Append "Origin" to the Vary header of the response
45 ### vary.append(header, field)
47 Adds the given header `field` to the `Vary` response header string `header`.
48 This can be a string of a single field, a string of a valid `Vary` header,
49 or an array of multiple fields.
51 This will append the header if not already listed, otherwise leaves
52 it listed in the current location. The new header string is returned.
54 <!-- eslint-disable no-undef -->
57 // Get header string appending "Origin" to "Accept, User-Agent"
58 vary.append('Accept, User-Agent', 'Origin')
63 ### Updating the Vary header when content is based on it
66 var http = require('http')
67 var vary = require('vary')
69 http.createServer(function onRequest (req, res) {
70 // about to user-agent sniff
71 vary(res, 'User-Agent')
73 var ua = req.headers['user-agent'] || ''
74 var isMobile = /mobi|android|touch|mini/i.test(ua)
76 // serve site, depending on isMobile
77 res.setHeader('Content-Type', 'text/html')
78 res.end('You are (probably) ' + (isMobile ? '' : 'not ') + 'a mobile user')
92 [npm-image]: https://img.shields.io/npm/v/vary.svg
93 [npm-url]: https://npmjs.org/package/vary
94 [node-version-image]: https://img.shields.io/node/v/vary.svg
95 [node-version-url]: https://nodejs.org/en/download
96 [travis-image]: https://img.shields.io/travis/jshttp/vary/master.svg
97 [travis-url]: https://travis-ci.org/jshttp/vary
98 [coveralls-image]: https://img.shields.io/coveralls/jshttp/vary/master.svg
99 [coveralls-url]: https://coveralls.io/r/jshttp/vary
100 [downloads-image]: https://img.shields.io/npm/dm/vary.svg
101 [downloads-url]: https://npmjs.org/package/vary