-
Notifications
You must be signed in to change notification settings - Fork 2
/
example-domain.js
128 lines (111 loc) · 3.45 KB
/
example-domain.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
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
var path = require('path'),
express = require ('express'),
mongoose = require("mongoose"),
domain = require("domain");
var app = express();
var PORT = process.env.PORT || 1026;
// Middleware to wrap request / response in a domain.
//
// Based on:
// * https://github.com/brianc/node-domain-middleware.
// * https://github.com/mathrawka/express-domain-errors
//
// Adds domain to res.locals so it can be bound to async operations outside of
// req/res (e.g., db queries).
//
// @param before [Function] Hook to run before error handling.
// @param after [Function] Hook to run after error handling.
//
var domainWrapper = function(before, after) {
return function(req, res, next) {
var reqDomain = domain.create();
res.locals._domain = reqDomain;
reqDomain.add(req);
reqDomain.add(res);
reqDomain.run(next);
reqDomain.once('error', function(err) {
if(before) before(err);
next(err);
if(after) after(err);
});
};
};
app.use(domainWrapper());
app.get("/domain", function(req, res) {
res.send(200, "Success");
});
var checkDomain = function(msg) {
if(!msg) {
msg = " ";
} else {
msg = " " + msg + " ";
};
console.log("process.domain" + msg + "exists:", process.domain !== undefined);
};
app.get("/domain/ex1", function(req, res) {
console.log("GET /domain/ex1: Connect default error handler will catch this.");
checkDomain();
throw new Error("ex");
});
app.get("/domain/ex2", function(req, res) {
console.log("GET /domain/ex2: domain will catch this.");
checkDomain("outside async");
var f = function() {
checkDomain("inside async");
throw new Error("ex");
};
setTimeout(f, 500);
});
app.get("/domain/ex3", function(req, res) {
console.log("GET /domain/ex3: domain will catch this.");
checkDomain("outside async");
var f = function() {
checkDomain("inside async");
throw new Error("ex");
};
process.nextTick(f);
});
app.get("/domain/ex4", function(req, res) {
console.log("GET /domain/ex4: This will send a response and then domain will catch this.");
checkDomain("outside async");
res.send(200);
var f = function() {
checkDomain("inside async");
throw new Error("ex");
};
process.nextTick(f); // or setImmediate
});
app.get("/domain/ex5", function(req, res) {
console.log("GET /domain/ex5: This will send a response and then will crash.");
checkDomain("outside async");
res.send(200);
var f = function() {
process.domain.dispose();
checkDomain("inside async");
throw new Error("ex");
};
process.nextTick(f); // or setImmediate
});
// Bonus: Mongoose.
mongoose.connect('mongodb://localhost:37017/test');
var AppModel = mongoose.model('AppModel', { field: String });
app.get("/domain/ex6", function(req, res) {
console.log("GET /domain/ex6: This will crash the server.");
checkDomain("outside findOne callback");
AppModel.findOne(function(err, doc) {
if (err) console.log("got error from mongoose", err);
checkDomain("inside findOne callback");
throw new Error("mongoose error");
});
});
app.get("/domain/ex7", function(req, res) {
console.log("GET /domain/ex7: This domain will catch this.");
checkDomain("outside findOne callback");
AppModel.findOne(process.domain.run(function(err, doc) {
if (err) console.log("got error from mongoose", err);
checkDomain("inside findOne callback");
throw new Error("mongoose error");
}));
});
app.listen(PORT);
console.log('Server running at http://127.0.0.1:' + PORT + '/');