1
1
#node-static-asset
2
2
3
3
node-static-asset is a static asset manager for Node.JS, designed for Express.
4
- This project aims to solve all application deployment problems. No thinking
5
- required .
4
+ This project aims to solve the problem of caching static assets (including
5
+ assets like * .js files that might change from time to time) .
6
6
7
7
## Background
8
8
9
- Google has a nice article about strong and weak caching. It's worth a quick
9
+ Google has a nice article about " strong" and " weak" caching. It's worth a quick
10
10
read if you don't know what that means.
11
11
12
12
https://developers.google.com/speed/docs/best-practices/caching
13
13
14
14
## Getting Started
15
15
16
- node-static-asset allows you to compile, bundle, minify/compress, and deploy
17
- your static assets. The basic idea works like this:
16
+ node-static-asset allows you to generate URL fingerprints for static assets in
17
+ a way that works for your application.
18
18
19
19
1 . Add the static-asset middleware to your Express stack
20
-
21
20
``` javascript
22
21
var staticAsset = require (' static-asset' );
23
22
app .use (staticAsset (__dirname + " /public/" ) );
24
23
```
25
24
26
25
2 . Get URL fingerprints of your static resources using ` req.assetFingerprint `
27
26
or the ` assetFingerprint ` view helper function.
28
-
29
27
``` javascript
30
28
app .get (" /info" , function (req , res , next ) {
31
29
res .type (" text/plain" ).send (" The URL fingerprint for jQuery is: " +
@@ -35,7 +33,9 @@ app.get("/info", function(req, res, next) {
35
33
36
34
The above gives you weak and strong caching for all files served in the /public
37
35
directory. But, that's not all; static-asset has many more features that allow
38
- you to customize how files are cached and how to generate URL fingerprints.
36
+ you to customize how files are cached and how to generate URL fingerprints. You
37
+ can even write your own middleware that minifies JavaScript files, for example,
38
+ and use static-asset to serve them to the browser.
39
39
40
40
## API
41
41
@@ -54,11 +54,13 @@ strong caching headers to the response.
54
54
55
55
** "Cache Strategy" Object** - a "cache strategy" object should implement one or
56
56
more of the following methods:
57
+
57
58
-lastModified(label_or_filename, cb) - a function that accepts a label
58
59
or filename and returns its last modified date to the callback.
59
60
If a last modified date could not be determined, null is passed to the
60
61
callback; otherwise, static-asset *may* use this Date to set the
61
62
`Last-Modified` HTTP header when the named resource is served.
63
+
62
64
-label_or_filename - a label or filename in `path`
63
65
-cb - a callback of the form `cb(err, lastModifiedDate)`
64
66
-etag(label_or_filename, cb) - Same as lastModified (above), except
@@ -77,6 +79,18 @@ strategy", return a URL fingerprint for the labelled resource, or if no such
77
79
label is registered, attempt to locate the specified filename within the ` path `
78
80
to determine its fingerprint.
79
81
82
+ ** req.assetFingerprint(label, urlFingerprint, cacheInfo)** - Registers a URL
83
+ fingerprint for the specified label.
84
+
85
+ -label - a label identifying the resource
86
+ -urlFingerprint - the URL fingerprint for the resource. If a request for this
87
+ resource is made, static-asset may add caching headers to the response.
88
+ -cacheInfo - an Object containing these properties:
89
+ -lastModified - the last modified date of the resource
90
+ -etag - the ETag of the resource
91
+ -expires - the expiration date of the resource; if not given, the resource
92
+ will expire approximately one year in the future. Set to ` null ` to
93
+ disable strong caching headers.
80
94
81
95
Other middleware on the stack can generate their own URL fingerprints for
82
96
static resources and expose them through ` req.assetFingerprint ` . Like this:
@@ -88,13 +102,6 @@ req.assetFingerprint(javascript_filename, javascript_filename + "?v=" +
88
102
stat .mdate .getTime (), {" lastModified" : stat .mdate });
89
103
```
90
104
91
- 4 . Customize your caching algorithms by overriding the defaults.
92
-
93
- ``` javascript
94
- var staticAsset = require (' static-asset' );
95
- app .use (staticAsset (__dirname + " /public/" ) );
96
- ```
97
-
98
105
## Install
99
106
100
107
` npm install static-asset `
0 commit comments