-
Notifications
You must be signed in to change notification settings - Fork 0
/
route-example.js
52 lines (47 loc) · 1.23 KB
/
route-example.js
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
48
49
50
51
52
var app = require('express')();
app.use(function(req, res, next) {
console.log('\n\nALLWAYS');
next();
});
app.get('/a', function(req, res) {
console.log('/a: route terminated');
res.send('a');
});
app.get('/a', function(req, res) {
console.log('/a: never called');
});
app.get('/b', function(req, res, next) {
console.log('/b: route not terminated');
next();
});
app.use(function(req, res, next) {
console.log('SOMETIMES');
next();
});
app.get('/b', function(req, res, next) {
console.log('/b (part 2): error thrown');
throw new Error('b failed');
});
app.use('/b', function(err, req, res, next) {
console.log('/b error detected and passed on');
next(err);
});
app.get('/c', function(err, req) {
console.log('/c: error thrown');
throw new Error('c failed');
});
app.use('/c', function(err, req, res, next) {
console.log('/c: error deteccted but not passed on');
next();
});
app.use(function(err, req, res, next) {
console.log('unhandled error detected: ' + err.message);
res.send('500 - server error');
});
app.use(function(req, res) {
console.log('route not handled');
res.send('404 - not found');
});
app.listen(3001, function() {
console.log('listening on 3001');
});