forked from olalonde/connectr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.js
50 lines (37 loc) · 1.29 KB
/
example2.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
var http = require('http'),
connect = require('connect'),
app = connect(),
connectr = require('./')(app);
var cookieParser = connect.cookieParser();
// we need to manually label middlewares
cookieParser.label = 'cookieParser';
app.use(cookieParser);
connectr.before('cookieParser').use(function (req, res, next) {
console.log('Middleware before cookie parser.');
next();
});
connectr.after('cookieParser').use(function (req, res, next) {
console.log('Middleware after cookie parser.');
next();
});
// you can also use connectr to label your middlewares
// instead of labeling them manually as above
connectr.use(connect.bodyParser()).as('bodyParser');
connectr.use(function (req, res, next) {
console.log('Last middleware');
res.end('Done!');
});
connectr.before('bodyParser').use(function (req, res, next) {
console.log('Before body parser');
next();
}).as('beforeBodyParser');
connectr.after('beforeBodyParser').use(function (req, res, next) {
console.log('I should be called after beforeBodyParser but before bodyParser');
next();
}).as('betweenBeforeBodyParserAndBodyParser');
connectr.after('bodyParser').use(function (req, res, next) {
console.log('After body parser');
next();
}).as('afterBodyParser');
//console.log(app.stack);
http.createServer(app).listen(3000);