Skip to content
Browse files

add default error-handling

  • Loading branch information...
1 parent 5444089 commit 2680dbddbac3203c5a48214c7406fe36697e09e1 @tj tj committed
Showing with 31 additions and 4 deletions.
  1. +4 −2 Readme.md
  2. +12 −2 lib/send.js
  3. +15 −0 test/send.js
View
6 Readme.md
@@ -41,6 +41,10 @@ var app = http.createServer(function(req, res){
- `stream` file streaming has started `(stream)`
- `end` streaming has completed
+## Error-handling
+
+ By default when no `error` listeners are present an automatic response will be made, otherwise you have full control over the response, aka you may show a 5xx page etc.
+
## About
Send is Connect's `static()` extracted for generalized use, a secure file
@@ -50,8 +54,6 @@ var app = http.createServer(function(req, res){
It does _not_ perform internal caching, you should use a reverse proxy cache such
as Varnish for this. If your application is small enough that it would benefit from single-node memory caching, it's small enough that it does not need caching at all ;).
- FUD: If you're performing pointless benchmarks, before complaining first consider that node-static does not respect cache-control directives and thus responds faster, but with invalid responses, use a real cache.
-
## Debugging
To enable `debug()` instrumentation output export __DEBUG__:
View
14 lib/send.js
@@ -4,12 +4,13 @@
*/
var debug = require('debug')('send')
- , fs = require('fs')
, parseRange = require('range-parser')
, Stream = require('stream')
, mime = require('mime')
, fresh = require('fresh')
, path = require('path')
+ , http = require('http')
+ , fs = require('fs')
, basename = path.basename
, normalize = path.normalize
, join = path.join
@@ -134,8 +135,17 @@ SendStream.prototype.maxage = function(ms){
SendStream.prototype.error = function(status, msg){
var err = new Error(msg);
+ var res = this.res;
err.status = status;
- this.emit('error', err);
+
+ if (this.listeners('error').length) {
+ this.emit('error', err);
+ return;
+ }
+
+ status = status || 500;
+ res.statusCode = status;
+ res.end(http.STATUS_CODES[status]);
};
/**
View
15 test/send.js
@@ -114,6 +114,21 @@ describe('send(file).pipe(res)', function(){
.end(done);
})
+ describe('when no "error" listeners are present', function(){
+ it('should respond to errors directly', function(done){
+ var app = http.createServer(function(req, res){
+ send('test/fixtures' + req.url)
+ .pipe(res);
+ });
+
+ request(app)
+ .get('/foobar')
+ .expect('Not Found')
+ .expect(404)
+ .end(done);
+ })
+ })
+
describe('with conditional-GET', function(){
it('should respond with 304 on a match', function(done){
request(app)

0 comments on commit 2680dbd

Please sign in to comment.
Something went wrong with that request. Please try again.