Skip to content

Commit 622917c

Browse files
committed
Add setHeaders option
closes #5
1 parent ee7f043 commit 622917c

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Add `setHeaders` option
45
* Include HTML link in redirect response
56
67
- Accept string for `maxAge` (converted by `ms`)

Readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Options:
3030
- `index` Default file name, defaults to `'index.html'`
3131
- `maxAge` Browser cache maxAge in milliseconds. This can also be a string accepted by the [ms](https://www.npmjs.org/package/ms#readme) module. defaults to `0`
3232
- `redirect` Redirect to trailing "/" when the pathname is a dir. defaults to `true`
33+
- `setHeaders` Function to set custom headers on response.
3334

3435
## Examples
3536

@@ -65,6 +66,25 @@ app.use(serveStatic('public/ftp', {'index': ['default.html', 'default.htm']}))
6566
app.listen(3000)
6667
```
6768

69+
### Serve all files as downloads
70+
71+
```js
72+
var express = require('express')
73+
var serveStatic = require('serve-static')
74+
75+
var app = express()
76+
77+
app.use(serveStatic('public/ftp', {
78+
'index': false,
79+
'setHeaders': setHeaders
80+
}))
81+
app.listen(3000)
82+
83+
function setHeaders(res, path) {
84+
res.attachment(path)
85+
}
86+
```
87+
6888
## License
6989

7090
The MIT License (MIT)

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ exports = module.exports = function(root, options){
5959
// default redirect
6060
var redirect = false !== options.redirect;
6161

62+
// headers listener
63+
var setHeaders = options.setHeaders
64+
delete options.setHeaders
65+
66+
if (setHeaders && typeof setHeaders !== 'function') {
67+
throw new TypeError('option setHeaders must be function')
68+
}
69+
6270
// setup options for send
6371
options.maxage = options.maxage || options.maxAge || 0;
6472
options.root = root;
@@ -94,6 +102,11 @@ exports = module.exports = function(root, options){
94102
stream.on('directory', next)
95103
}
96104

105+
// add headers listener
106+
if (setHeaders) {
107+
stream.on('headers', setHeaders)
108+
}
109+
97110
// forward non-404 errors
98111
stream.on('error', function error(err) {
99112
next(err.status === 404 ? null : err)

test/test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,51 @@ describe('serveStatic()', function(){
190190
})
191191
})
192192

193+
describe('setHeaders', function () {
194+
it('should reject non-functions', function () {
195+
serveStatic.bind(null, fixtures, {'setHeaders': 3}).should.throw(/setHeaders.*function/)
196+
})
197+
198+
it('should get called when sending file', function(done){
199+
var server = createServer(fixtures, {'setHeaders': function (res) {
200+
res.setHeader('x-custom', 'set')
201+
}})
202+
203+
request(server)
204+
.get('/nums')
205+
.expect('x-custom', 'set')
206+
.expect(200, done)
207+
})
208+
209+
it('should not get called on 404', function(done){
210+
var server = createServer(fixtures, {'setHeaders': function (res) {
211+
res.setHeader('x-custom', 'set')
212+
}})
213+
214+
request(server)
215+
.get('/bogus')
216+
.expect(404, function (err, res) {
217+
if (err) return done(err)
218+
res.headers.should.not.have.property('x-custom')
219+
done()
220+
})
221+
})
222+
223+
it('should not get called on redirect', function(done){
224+
var server = createServer(fixtures, {'setHeaders': function (res) {
225+
res.setHeader('x-custom', 'set')
226+
}})
227+
228+
request(server)
229+
.get('/users')
230+
.expect(303, function (err, res) {
231+
if (err) return done(err)
232+
res.headers.should.not.have.property('x-custom')
233+
done()
234+
})
235+
})
236+
})
237+
193238
describe('when traversing passed root', function(){
194239
var server;
195240
before(function () {

0 commit comments

Comments
 (0)