README.md (2716B)
1 # vary 2 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] 8 9 Manipulate the HTTP Vary header 10 11 ## Installation 12 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): 16 17 ```sh 18 $ npm install vary 19 ``` 20 21 ## API 22 23 <!-- eslint-disable no-unused-vars --> 24 25 ```js 26 var vary = require('vary') 27 ``` 28 29 ### vary(res, field) 30 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. 34 35 This will append the header if not already listed, otherwise leaves 36 it listed in the current location. 37 38 <!-- eslint-disable no-undef --> 39 40 ```js 41 // Append "Origin" to the Vary header of the response 42 vary(res, 'Origin') 43 ``` 44 45 ### vary.append(header, field) 46 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. 50 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. 53 54 <!-- eslint-disable no-undef --> 55 56 ```js 57 // Get header string appending "Origin" to "Accept, User-Agent" 58 vary.append('Accept, User-Agent', 'Origin') 59 ``` 60 61 ## Examples 62 63 ### Updating the Vary header when content is based on it 64 65 ```js 66 var http = require('http') 67 var vary = require('vary') 68 69 http.createServer(function onRequest (req, res) { 70 // about to user-agent sniff 71 vary(res, 'User-Agent') 72 73 var ua = req.headers['user-agent'] || '' 74 var isMobile = /mobi|android|touch|mini/i.test(ua) 75 76 // serve site, depending on isMobile 77 res.setHeader('Content-Type', 'text/html') 78 res.end('You are (probably) ' + (isMobile ? '' : 'not ') + 'a mobile user') 79 }) 80 ``` 81 82 ## Testing 83 84 ```sh 85 $ npm test 86 ``` 87 88 ## License 89 90 [MIT](LICENSE) 91 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