middleware.js
1.58 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
var express = require('express'),
http = require('http'),
errors = require('../..');
function createServer(port, errorHandler) {
var app = exports.app = express();
app.set('port', port);
app.use(function(req, res, next) {
if (req instanceof Error) {
return next(err);
}
if (req.get('X-API-Key') == 'abc123') {
return next();
}
throw new errors.Http401Error(
'Invalid X API Key'
, 'The X API Key provided is invalid or expired'
, 'Specify a valid X-API-Key header and retry the request');
});
app.use(errorHandler);
app.use(app.router);
app.get('/echo/:msg', function(req, res) {
res.send(req.params.msg);
});
http.createServer(app).listen(app.get('port'), function() {
var port = app.get('port');
console.log("Express server listening on port " + port);
console.log("API available on http://localhost:" + port + "/echo/:msg");
});
}
// connect middleware - works fine, but not as pretty
// due to the nature of the custom toString() on our errors
createServer(3942, express.errorHandler());
//express.errorHandler.title = 'Out Of The Box Connect';
// errors middleware in connect compat mode
// looks just like a vanilla connect.errorHandler response
createServer(3943, errors.errorHandler({connectCompat: true, title: 'Connect Compat'}));
// errors flavor middleware - preferred
// contains complete custom error details
createServer(3944, errors.errorHandler({title: 'Errors Middleware', includeStack: true}));