Skip to content

Commit

Permalink
Add default 400 and 500 middleware to stack automatically
Browse files Browse the repository at this point in the history
(instead of putting them in the explicit middleware order)

We'll update the default `config/http.js` file to remove 404 and 500 from the order.
  • Loading branch information
sgress454 committed Nov 15, 2016
1 parent 176e085 commit a22e4e7
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 6 deletions.
3 changes: 0 additions & 3 deletions lib/hooks/http/get-configured-http-middleware-fns.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,6 @@ module.exports = function getBuiltInHttpMiddleware (expressRouterMiddleware, sai

// 404 and 500 middleware should be attached at the very end
// (after `router`, `www`, and `favicon`)
//
// * TODO: make these both implicit (we can just tack them on to the end of the
// * middleware stack automatically--it's confusing that they appear to be configurable)
404: function handleUnmatchedRequest(req, res, next) {

// Explicitly ignore error arg to avoid inadvertently
Expand Down
4 changes: 1 addition & 3 deletions lib/hooks/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ module.exports = function(sails) {
'$custom',
'router',
'www',
'favicon',
'404',
'500'
'favicon'
],

// Built-in HTTP middleware functions are injected after the express
Expand Down
6 changes: 6 additions & 0 deletions lib/hooks/http/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ module.exports = function(sails) {
}
});

// Add the default 404 to the middleware order
postRouterMiddleware.push('404');

// Add the default 500 to the middleware order
postRouterMiddleware.push('500');

// If a custom `loadMiddleware` function was configured, then call it to "use"
// the configured middleware (instead of doing it automatically with the more
// modern `sails.config.http.middleware.order` configuration).
Expand Down
196 changes: 196 additions & 0 deletions test/integration/middleware.404.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
var _ = require('@sailshq/lodash');
var request = require('request');
var Sails = require('../../lib').Sails;
var assert = require('assert');
var fs = require('fs-extra');
var request = require('request');
var appHelper = require('./helpers/appHelper');
var path = require('path');

describe('middleware :: ', function() {

describe('404 :: ', function() {

var appName = 'testApp';
var sailsApp;

before(function(done) {
appHelper.build(function(err) {
if (err) {return done(err);}
fs.writeFileSync(path.resolve('..', appName, 'views', '404.ejs'), 'no file here bruh!');
return done();
});
});

after(function() {
process.chdir('../');
appHelper.teardown();
});

describe('with no custom 404 handler installed', function() {

before(function(done) {
appHelper.lift({
hooks: {
pubsub: false
}
}, function(err, _sailsApp) {
if (err) { return done(err); }
sailsApp = _sailsApp;
return done();
});
});

it('the default 404 handler should respond to a request for an unbound URL', function(done) {

request(
{
method: 'GET',
uri: 'http://localhost:1342/nothing',
headers: {
'Accept': 'text/html'
}
},
function(err, response, body) {
if (err) { return done(err); }
assert.equal(response.statusCode, 404);
assert(body.match('<html>'));
assert(body.match('no file here bruh!'));
return done();
}
);

});


after(function(done) {
sailsApp.lower(done);
});

});

describe('with a custom 404 handler installed', function() {

before(function(done) {
appHelper.lift({
hooks: {
pubsub: false
},
http: {
middleware: {
order: [
'startRequestTimer',
'cookieParser',
'session',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon',
'notfound'
],
notfound: function (req, res) {
return res.send('custom nada bro');
}
}
}
}, function(err, _sailsApp) {
if (err) { return done(err); }
sailsApp = _sailsApp;
return done();
});
});

it('the custom 404 handler should respond to a request for an unbound URL', function(done) {

request(
{
method: 'GET',
uri: 'http://localhost:1342/nothing',
headers: {
'Accept': 'text/html'
}
},
function(err, response, body) {
if (err) { return done(err); }
assert.equal(response.statusCode, 200);
assert.equal(body, 'custom nada bro');
return done();
}
);

});


after(function(done) {
sailsApp.lower(done);
});

});

describe('with 404 left out of a custom middleware order', function() {

before(function(done) {
appHelper.lift({
hooks: {
pubsub: false
},
http: {
middleware: {
order: [
'startRequestTimer',
'cookieParser',
'session',
'bodyParser',
'handleBodyParserError',
'compress',
'methodOverride',
'poweredBy',
'$custom',
'router',
'www',
'favicon'
]
}
}
}, function(err, _sailsApp) {
if (err) { return done(err); }
sailsApp = _sailsApp;
return done();
});
});

it('the default 404 handler should still respond to a request for an unbound URL', function(done) {

request(
{
method: 'GET',
uri: 'http://localhost:1342/nothing',
headers: {
'Accept': 'text/html'
}
},
function(err, response, body) {
if (err) { return done(err); }
assert.equal(response.statusCode, 404);
assert(body.match('<html>'));
assert(body.match('no file here bruh!'));
return done();
}
);

});

after(function(done) {
sailsApp.lower(done);
});

});

});

});
Loading

0 comments on commit a22e4e7

Please sign in to comment.