vary
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The npm package "vary" is designed to manipulate the HTTP 'Vary' header. It is a helpful tool when dealing with HTTP caching, as it allows the server to communicate which headers it uses to determine how a response is generated. The 'Vary' header essentially aids in making HTTP caching more efficient.
To use the vary package, you first need to install it using npmโs install command:
$ npm install vary
After its installation, you make use of it by requiring it within your JavaScript file:
var vary = require('vary');
Following this, if you have a header field that you want to add to the Vary response header, you can utilize the vary function:
// Appending "Origin" to the Vary header of the response
vary(res, 'Origin');
For an expanded perspective, if you're generating content based on a certain header, like the 'User-Agent' in this example, you can add it to the 'Vary' header for efficiency:
var http = require('http');
var vary = require('vary');
http.createServer(function onRequest (req, res) {
// If you're about to user-agent sniff
vary(res, 'User-Agent');
var ua = req.headers['user-agent'] || '';
var isMobile = /mobi|android|touch|mini/i.test(ua);
// You may want to serve different content based on the user's device
res.setHeader('Content-Type', 'text/html');
res.end('You are (probably) ' + (isMobile ? '' : 'not ') + 'a mobile user');
});
The documentation for the vary npm package can be found on its GitHub page. The README file on this page provides a detailed explanation of how to use the package, complete with installation steps and API documentation.