Skip to content

Commit 6f87aaa

Browse files
committed
add documentation for cache and modified methods
1 parent 66230ad commit 6f87aaa

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

README.md

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Lambda API was written to be extremely lightweight and built specifically for se
5252
- [REQUEST](#request)
5353
- [RESPONSE](#response)
5454
- [attachment()](#attachmentfilename)
55+
- [cache()](#cacheage-private)
5556
- [clearCookie()](#clearcookiename-options)
5657
- [cookie()](#cookiename-value-options)
5758
- [cors()](#corsoptions)
@@ -65,6 +66,7 @@ Lambda API was written to be extremely lightweight and built specifically for se
6566
- [json()](#jsonbody)
6667
- [jsonp()](#jsonpbody)
6768
- [location](#locationpath)
69+
- [modified()](#modifieddate)
6870
- [redirect()](#redirectstatus-path)
6971
- [removeHeader()](#removeheaderkey)
7072
- [send()](#sendbody)
@@ -332,8 +334,8 @@ The `REQUEST` object contains a parsed and normalized request from API Gateway.
332334
- `headers`: An object containing the request headers (properties converted to lowercase for HTTP/2, see [rfc7540 8.1.2. HTTP Header Fields](https://tools.ietf.org/html/rfc7540))
333335
- `rawHeaders`: An object containing the original request headers (property case preserved)
334336
- `body`: The body of the request. If the `isBase64Encoded` flag is `true`, it will be decoded automatically.
335-
- If the `Content-Type` header is `application/json`, it will attempt to parse the request using `JSON.parse()`
336-
- If the `Content-Type` header is `application/x-www-form-urlencoded`, it will attempt to parse a URL encoded string using `querystring`
337+
- If the `content-type` header is `application/json`, it will attempt to parse the request using `JSON.parse()`
338+
- If the `content-type` header is `application/x-www-form-urlencoded`, it will attempt to parse a URL encoded string using `querystring`
337339
- Otherwise it will be plain text.
338340
- `rawBody`: If the `isBase64Encoded` flag is `true`, this is a copy of the original, base64 encoded body
339341
- `route`: The matched route of the request
@@ -359,11 +361,11 @@ api.get('/users', (req,res) => {
359361
```
360362

361363
### header(key, value)
362-
The `header` method allows for you to set additional headers to return to the client. By default, just the `Content-Type` header is sent with `application/json` as the value. Headers can be added or overwritten by calling the `header()` method with two string arguments. The first is the name of the header and then second is the value.
364+
The `header` method allows for you to set additional headers to return to the client. By default, just the `content-type` header is sent with `application/json` as the value. Headers can be added or overwritten by calling the `header()` method with two string arguments. The first is the name of the header and then second is the value.
363365

364366
```javascript
365367
api.get('/users', (req,res) => {
366-
res.header('Content-Type','text/html').send('<div>This is HTML</div>')
368+
res.header('content-type','text/html').send('<div>This is HTML</div>')
367369
})
368370
```
369371

@@ -429,7 +431,7 @@ api.get('/users', (req,res) => {
429431
```
430432

431433
### type(type)
432-
Sets the `Content-Type` header for you based on a single `String` input. There are thousands of MIME types, many of which are likely never to be used by your application. Lambda API stores a list of the most popular file types and will automatically set the correct `Content-Type` based on the input. If the `type` contains the "/" character, then it sets the `Content-Type` to the value of `type`.
434+
Sets the `content-type` header for you based on a single `String` input. There are thousands of MIME types, many of which are likely never to be used by your application. Lambda API stores a list of the most popular file types and will automatically set the correct `content-type` based on the input. If the `type` contains the "/" character, then it sets the `content-type` to the value of `type`.
433435

434436
```javascript
435437
res.type('.html'); // => 'text/html'
@@ -494,7 +496,7 @@ Defaults can be set by calling `res.cors()` with no properties, or with any comb
494496
res.cors({
495497
origin: 'example.com',
496498
methods: 'GET, POST, OPTIONS',
497-
headers: 'Content-Type, Authorization',
499+
headers: 'content-type, authorization',
498500
maxAge: 84000000
499501
})
500502
```
@@ -553,8 +555,20 @@ res.clearCookie('fooArray', { path: '/', httpOnly: true }).send()
553555
### etag([boolean])
554556
Enables Etag generation for the response if at value of `true` is passed in. Lambda API will generate an Etag based on the body of the response and return the appropriate header. If the request contains an `If-No-Match` header that matches the generated Etag, a `304 Not Modified` response will be returned with a blank body.
555557

558+
### cache([age][,private])
559+
Adds `cache-control` header to responses. If the first parameter is an `integer`, it will add a `max-age` to the header. The number should be in milliseconds. If the first parameter is `true`, it will add the cache headers with `max-age` set to `0` and use the current time for the `expires` header. If set to false, it will add a cache header with `no-cache, no-store, must-revalidate` as the value. You can also provide a custom string that will manually set the value of the `cache-control` header. And optional second argument takes a `boolean` and will set the `cache-control` to `private` This method is chainable.
560+
561+
```javascript
562+
res.cache(false).send() // 'cache-control': 'no-cache, no-store, must-revalidate'
563+
res.cache(1000).send() // 'cache-control': 'max-age=1'
564+
res.cache(30000,true).send() // 'cache-control': 'private, max-age=30'
565+
```
566+
567+
### modified(date)
568+
Adds a `last-modified` header to responses. A value of `true` will set the value to the current date and time. A JavaScript `Date` object can also be passed in. Note that it will be converted to UTC if not already. A `string` can also be passed in and will be converted to a date if JavaScript's `Date()` function is able to parse it. A value of `false` will prevent the header from being generated, but will not remove any existing `last-modified` headers.
569+
556570
### attachment([filename])
557-
Sets the HTTP response `Content-Disposition` header field to "attachment". If a `filename` is provided, then the `Content-Type` is set based on the file extension using the `type()` method and the "filename=" parameter is added to the `Content-Disposition` header.
571+
Sets the HTTP response `content-disposition` header field to "attachment". If a `filename` is provided, then the `content-type` is set based on the file extension using the `type()` method and the "filename=" parameter is added to the `content-disposition` header.
558572

559573
```javascript
560574
res.attachment()
@@ -566,7 +580,7 @@ res.attachment('path/to/logo.png')
566580
```
567581

568582
### download(file [, filename] [, options] [, callback])
569-
This transfers the `file` (either a local path, S3 file reference, or Javascript `Buffer`) as an "attachment". This is a convenience method that combines `attachment()` and `sendFile()` to prompt the user to download the file. This method optionally takes a `filename` as a second parameter that will overwrite the "filename=" parameter of the `Content-Disposition` header, otherwise it will use the filename from the `file`. An optional `options` object passes through to the [sendFile()](#sendfilefile--options--callback) method and takes the same parameters. Finally, a optional `callback` method can be defined which is passed through to [sendFile()](#sendfilefile--options--callback) as well.
583+
This transfers the `file` (either a local path, S3 file reference, or Javascript `Buffer`) as an "attachment". This is a convenience method that combines `attachment()` and `sendFile()` to prompt the user to download the file. This method optionally takes a `filename` as a second parameter that will overwrite the "filename=" parameter of the `content-disposition` header, otherwise it will use the filename from the `file`. An optional `options` object passes through to the [sendFile()](#sendfilefile--options--callback) method and takes the same parameters. Finally, a optional `callback` method can be defined which is passed through to [sendFile()](#sendfilefile--options--callback) as well.
570584

571585
```javascript
572586
res.download('./files/sales-report.pdf')
@@ -589,10 +603,10 @@ The `sendFile()` method takes up to three arguments. The first is the `file`. Th
589603
| -------- | ---- | ----------- | ------- |
590604
| maxAge | `Number` | Set the expiration time relative to the current time in milliseconds. Automatically sets the `Expires` header | 0 |
591605
| root | `String` | Root directory for relative filenames. | |
592-
| lastModified | `Boolean` or `String` | Sets the `Last-Modified` header to the last modified date of the file. This can be disabled by setting it to `false`, or overridden by setting it to a valid `Date` object | |
606+
| lastModified | `Boolean` or `String` | Sets the `last-modified` header to the last modified date of the file. This can be disabled by setting it to `false`, or overridden by setting it to a valid `Date` object | |
593607
| headers | `Object` | Key value pairs of additional headers to be sent with the file | |
594-
| cacheControl | `Boolean` or `String` | Enable or disable setting `Cache-Control` response header. Override value with custom string. | true |
595-
| private | `Boolean` | Sets the `Cache-Control` to `private`. | false |
608+
| cacheControl | `Boolean` or `String` | Enable or disable setting `cache-control` response header. Override value with custom string. | true |
609+
| private | `Boolean` | Sets the `cache-control` to `private`. | false |
596610

597611
```javascript
598612
res.sendFile('./img/logo.png')
@@ -662,7 +676,7 @@ Middleware can be used to authenticate requests, log API calls, etc. The `REQUES
662676
```javascript
663677
// Auth User
664678
api.use((req,res,next) => {
665-
if (req.headers.Authorization === 'some value') {
679+
if (req.headers.authorization === 'some value') {
666680
req.authorized = true
667681
next() // continue execution
668682
} else {

0 commit comments

Comments
 (0)