Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
docs: add Node.js server example
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 22, 2017
1 parent 0417c54 commit fec5fe4
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ links to further reading on this Safari bug.

## Example

### API usage

```js
var reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { 'etag': '"bar"' }
Expand All @@ -65,6 +67,36 @@ fresh(reqHeaders, resHeaders)
// => true
```

### Using with Node.js http server

```js
var fresh = require('fresh')
var http = require('http')

var server = http.createServer(function (req, res) {
// perform server logic
// ... including adding ETag / Last-Modified response headers

if (isFresh(req, res)) {
// client has a fresh copy of resource
res.statusCode = 304
res.end()
return
}

// send the resource
})

function isFresh (req, res) {
return fresh(req.headers, {
'etag': res.getHeader('ETag'),
'last-modified': res.getHeader('Last-Modified')
})
}

server.listen(3000)
```

## License

[MIT](LICENSE)
Expand Down

0 comments on commit fec5fe4

Please sign in to comment.