forked from aseemk/express-streamline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample._js
More file actions
135 lines (117 loc) · 3.5 KB
/
Copy pathexample._js
File metadata and controls
135 lines (117 loc) · 3.5 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// example.js
// Either run directly, or require() for testing.
//
// Run directly (defaults to random port):
// node example.js [<port>]
//
// Require for testing:
// var app = require('./example');
// app.listen(port, function () { ... });
//
// Examples:
// - GET / - basic async hello world
// - GET /delay/5, /delay/50, etc. - return response after 5, 50, etc. ms
// - GET /error - return 500 from route error
// - GET /anything?middleware=error - return 500 from middleware error
// - GET /anything?middleware=stop - return 200 from middleware
// - GET /next - return 200 from fall-through routes
// - GET /error/param - return 500 from param error
// - PATCH /resource - return 200 from PATCH handler
// - GET /global - return global context from GET handler
// - GET /global?value='foo' - set global context from middleware and
// return from route handler".
//
var crypto = require('crypto');
var express = require('./');
var app = express();
var streamlineGlobal = require('streamline/lib/globals');
app.use(express.logger('dev'));
app.use(express.responseTime());
// Middleware example (normal):
app.use(function (req, res, _) {
res.header('X-Request-Id', crypto.randomBytes(16, _).toString('hex'));
});
// Middleware example (error, or don't continue):
app.use(function (req, res, _) {
setTimeout(_, 5);
switch (req.query['middleware']) {
case 'error':
throw new Error('Middleware error.');
case 'stop':
res.send('Middleware stopped.');
return false;
default:
return;
}
});
// Example of middleware setting streamline global context
app.use(function (req, res, _) {
queryValue = req.query['value']
if (queryValue) {
streamlineGlobal.context['value'] = queryValue;
}
});
// Route example (normal):
app.get('/', function (req, res, _) {
setTimeout(_, 5);
res.send('Hello world!');
});
// Param example (normal):
app.param('ms', function (req, res, _, ms) {
if (ms.match(/\d+/)) {
setTimeout(_, 5);
req.ms = parseInt(ms, 10);
}
});
app.get('/delay/:ms', function (req, res, _) {
setTimeout(_, req.ms);
res.send('Hello world after ' + req.ms + 'ms!');
});
// Route example (error):
app.get('/error', function (req, res, _) {
setTimeout(_, 5);
throw new Error('Route error.');
});
// Route example (fall-through):
app.get('/next', function (req, res, _) {
setTimeout(_, 5);
return true;
});
app.get('/next', function (req, res, _) {
setTimeout(_, 5);
res.send('Fell through to another matching route.');
});
// Param example (error):
app.param('err', function (req, res, _, err) {
if (err === 'param') {
setTimeout(_, 5);
throw new Error('Param error.');
}
});
app.get('/error/:err', function (req, res, _) {
throw new Error('This should never get hit.');
});
// Example of method (verb) coverage:
app.patch('/resource', function (req, res, _) {
setTimeout(_, 5);
res.send('Resource patched.');
});
// Example of error handler:
app.use(function (err, req, res, _) {
setTimeout(_, 5);
res.send(500, err.message);
});
// Example of retrieving streamline global context
app.get('/global', function (req, res, _) {
setTimeout(_, 5);
res.send(streamlineGlobal.context);
});
module.exports = app;
if (module === require.main) {
app.listen(process.argv[2] || 0, function () {
console.log(
'express-streamline example server listening at http://localhost:%d/...',
this.address().port
);
});
}