forked from tdebarochez/connect-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
34 lines (32 loc) · 1010 Bytes
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var cache = require('./lib/connect-cache');
var connect = require('connect');
function helloWorld(req, res) {
if (req.url == '/') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('direct result');
res.end('');
}
else if (req.url == '/path') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
setTimeout(function () {
res.write('cached result');
res.end('');
}, 1000);
}
else if (req.url == '/test.jpg') {
var img = require('fs').readFileSync('static/test.jpg', 'binary');
res.writeHead(200,{'Content-Type': 'image/jpeg',
'Content-Length': img.length});
res.write(img, 'binary');
res.end();
}
else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.write('not found');
res.end('');
}
}
var server = connect.createServer(cache({regex: /path.*|test.jpg/,
ttl: 60000}),
helloWorld);
server.listen(3000);