Connect Stream
Serving static file with the given path.
Inspired by isaacs/st.
- compatible with return code
200
, 206
, 304
, 403
, 404
, 500
- cache fd, stat and gzip content
- gzip deflatable
- useful to serve movie to mobile device
Install
$ npm install connect-stream
Usage
Include in your project:
var stream = require('connect-stream');
app.use(stream(path.resolve('public')));
Use in your routes:
app.get(function (req, res) {
return res.stream('movie.mp4');
});
- It returns
200
if normal request. - It returns
206
if partial request. - It returns
304
if ETag or Modified-Since matched. - It returns
404
if movie.mp4
not exists. - It returns
403
if movie.mp4
permission denied. - It returns
500
if error.
How to use
connect-stream
respond to Range Request (HTTP 206) correctly.
14.35 Range - W3, RFC2616
Here are all the options described with their defaults values and a few possible settings you might choose to use:
stream = require('connect-stream');
app.use(stream(path.resolve('public'), {
trim: false,
trim: true,
concatenate: 'resolve',
concatenate: 'join',
passthrough: true,
passthrough: false,
cache: {
fd: {
max: 1000,
maxAge: 1000 * 60 * 60,
},
stat: {
max: 5000,
maxAge: 1000 * 60,
},
content: {
max: 1024 * 1024 * 64,
maxAge: 1000 * 60 * 10,
}
}
}));
var callback = function (err, range, isFirstStream) {
console.error(err);
console.log(range);
console.log(isFirstStream);
};
app.get(/^\/(.*)\.mp4$/, function (req, res) {
res.stream(req.params[0] + '.mp4', callback);
});
var options = {
headers: {
'content-type': 'application/octet-stream',
'cache-control': 'public'
}
};
app.get(/^\/download\/(.*)\.mp4$/, function (req, res) {
res.stream(req.params[0] + '.mp4', options, callback);
});
Upgrade Guide (0.x -> 1.x)
interface
app.use(require('connect-stream'));
app.use(require('connect-stream')());
behavior
var stream = require('connect-stream');
app.use(stream(path.resolve('public')));
app.get(function (req, res) {
res.stream('/tmp/a.mp4');
});
var stream = require('connect-stream');
app.use(stream(path.resolve('public'), {
concatenate: 'resolve'
});
app.get(function (req, res) {
res.stream('/tmp/a.mp4');
});
Tips
count up your database on range request (e.g. playing movie)
range-request requests sequential at one request.
you should use filter in callback for count up play-times.
Example:
app.get('/movie.mp4', function (req, res) {
res.stream('movie.mp4', function (err, range, isFirstStream) {
if (isFirstStream) {
countUpYourDatabaseHere();
}
});
});
MIT LICENSE
Copyright © 2013 geta6 licensed under MIT
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.