fresh
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The Fresh package is a Node.js module that helps with HTTP response freshness testing. It checks the freshness of a response using client and server headers. If the client's cache still possesses a fresh version of the resource, the module returns true. On the contrary, if the client's cache is stale, it returns false, indicating that a full response should be sent again. The Fresh module adheres strictly to HTTP specifications and doesn't aim to resolve all client bugs. However, for certain known issues, such as Safari-related caching problems, reference to another module like jumanji is suggested.
After installing the npm module fresh with the command npm install fresh
, it can be implemented into your JS project with the following syntax:
const fresh = require('fresh')
let reqHeaders = { 'if-none-match': '"foo"' }
let resHeaders = { 'etag': '"bar"' }
console.log(fresh(reqHeaders, resHeaders)) // Output: false
reqHeaders = { 'if-none-match': '"foo"' }
resHeaders = { 'etag': '"foo"' }
console.log(fresh(reqHeaders, resHeaders)) // Output: true
In the above example, fresh
is checking the request and response headers' freshness. If the headers match, it returns true
, implying that the client still contains a fresh version of the resource, else it returns false
.
For implementation with Node.js HTTP server:
const fresh = require('fresh')
const http = require('http')
const server = http.createServer((req, res) => {
/* perform server logic, including adding ETag / Last-Modified response headers */
if (isFresh(req, res)) {
// if client has a fresh copy of the resource
res.statusCode = 304
res.end()
return
}
// Else, send the resource
res.statusCode = 200
res.end('hello, world!')
})
const isFresh = (req, res) => {
return fresh(req.headers, {
'etag': res.getHeader('ETag'),
'last-modified': res.getHeader('Last-Modified')
})
}
server.listen(3000)
The example shows the integration of the fresh module with a Node.js HTTP server. The function isFresh
checks the request and response headers to verify if the client's cache contains a fresh copy of the resource or not.
The Fresh module documentation can be found within its npm registry. The README file provides comprehensive coverage of its functionality, known issues, and examples of how to use it. The fresh module follows the HTTP standards strictly and checks the freshness of the HTTP response based on the request and response headers. This information ensures optimal design and implementation of server-client communication by adhering to best practices for resource freshness, cache control, and HTTP response handling.