fresh模块 - Node.js开发参考——模具行业从业者必备知识手册
-
<h2>fresh</h2>
<p>[![NPM Version][npm-image]][npm-url]</p>
<p>[![NPM Downloads][downloads-image]][downloads-url]</p>
<p>[![Node.js Version][node-version-image]][node-version-url]</p>
<p>[![Build Status][travis-image]][travis-url]</p>
<p>[![Test Coverage][coveralls-image]][coveralls-url]</p><p>HTTP response freshness testing</p>
<h3>Installation</h3>
<p>This is a <a href="https://nodejs.org/en/">Node.js</a> module available through the</p>
<p><a href="https://www.npmjs.com/">npm registry</a>. Installation is done using the</p>
<p><a href="https://docs.npmjs.com/getting-started/installing-npm-packages-locally"><code>npm install</code> command</a>:</p><pre><code>
$ npm install fresh
</code></pre><h3>API</h3>
<p><!-- eslint-disable no-unused-vars --></p>
<pre><code>
var fresh = require('fresh')
</code></pre><h4>fresh(reqHeaders, resHeaders)</h4>
<p>Check freshness of the response using request and response headers.</p>
<p>When the response is still "fresh" in the client's cache <code>true</code> is</p>
<p>returned, otherwise <code>false</code> is returned to indicate that the client</p>
<p>cache is now stale and the full response should be sent.</p><p>When a client sends the <code>Cache-Control: no-cache</code> request header to</p>
<p>indicate an end-to-end reload request, this module will return <code>false</code></p>
<p>to make handling these requests transparent.</p><h3>Known Issues</h3>
<p>This module is designed to only follow the HTTP specifications, not</p>
<p>to work-around all kinda of client bugs (especially since this module</p>
<p>typically does not recieve enough information to understand what the</p>
<p>client actually is).</p><p>There is a known issue that in certain versions of Safari, Safari</p>
<p>will incorrectly make a request that allows this module to validate</p>
<p>freshness of the resource even when Safari does not have a</p>
<p>representation of the resource in the cache. The module</p>
<p><a href="https://www.npmjs.com/package/jumanji">jumanji</a> can be used in</p>
<p>an Express application to work-around this issue and also provides</p>
<p>links to further reading on this Safari bug.</p><h3>Example</h3>
<h4>API usage</h4>
<p><!-- eslint-disable no-redeclare, no-undef --></p>
<pre><code>
var reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { 'etag': '"bar"' }
fresh(reqHeaders, resHeaders)
// => falsevar reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { 'etag': '"foo"' }
fresh(reqHeaders, resHeaders)
// => true
</code></pre><h4>Using with Node.js http server</h4>
<pre><code>
var fresh = require('fresh')
var http = require('http')var server = http.createServer(function (req, res) {
// perform server logic
// ... including adding ETag / Last-Modified response headersif (isFresh(req, res)) {
// client has a fresh copy of resource
res.statusCode = 304
res.end()
return
}// send the resource
res.statusCode = 200
res.end('hello, world!')
})function isFresh (req, res) {
return fresh(req.headers, {
'etag': res.getHeader('ETag'),
'last-modified': res.getHeader('Last-Modified')
})
}server.listen(3000)
</code></pre><h3>License</h3>
<p><a href="LICENSE">MIT</a></p>
<p>[npm-image]: https://img.shields.io/npm/v/fresh.svg</p>
<p>[npm-url]: https://npmjs.org/package/fresh</p>
<p>[node-version-image]: https://img.shields.io/node/v/fresh.svg</p>
<p>[node-version-url]: https://nodejs.org/en/</p>
<p>[travis-image]: https://img.shields.io/travis/jshttp/fresh/master.svg</p>
<p>[travis-url]: https://travis-ci.org/jshttp/fresh</p>
<p>[coveralls-image]: https://img.shields.io/coveralls/jshttp/fresh/master.svg</p>
<p>[coveralls-url]: https://coveralls.io/r/jshttp/fresh?branch=master</p>
<p>[downloads-image]: https://img.shields.io/npm/dm/fresh.svg</p>
<p>[downloads-url]: https://npmjs.org/package/fresh</p><hr>
<p>本文由模极社发布</p>
<hr>
<h4>
你可能还感兴趣</h4>
<ul>
<li><a href="/t/4192/4192">2026年4月工作计划</a></li>
<li><a href="/t/3674/3674">PPO(聚苯醚)</a></li>
<li><a href="/t/2840/2840">动模座板功能说明与设计选型指南</a></li>
</ul><hr>
<p>你在实际生产中遇到过类似问题吗?欢迎留言讨论。</p>