Skip to content

Commit 322b1c2

Browse files
committed
express dependency splitted into it's own module
1 parent b4748e2 commit 322b1c2

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

examples/express.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ var express = require('express');
66
var connString = 'postgres://postgres:postgres@localhost/miniprofiler';
77

88
var app = express();
9-
app.use(miniprofiler.profile());
9+
app.use(miniprofiler.express());
1010
app.use(miniprofiler.for.pg(pg));
1111
app.use(miniprofiler.for.redis(redis));
1212

13-
var redisClient = redis.createClient();
14-
1513
app.set('view engine', 'pug');
1614
app.set('views', './examples/views');
1715

@@ -30,6 +28,7 @@ app.get('/js-sleep', function(req, res) {
3028
});
3129

3230
app.get('/redis-set-get', function(req, res) {
31+
var redisClient = redis.createClient();
3332
redisClient.set('customer', '[email protected]', function() {
3433
redisClient.get('customer', function(err, reply) {
3534
res.render('home');
@@ -47,6 +46,7 @@ app.get('/pg-sleep', function(req, res) {
4746
});
4847

4948
app.get('/all', function(req, res) {
49+
var redisClient = redis.createClient();
5050
req.miniprofiler.step('Waiting 1 second', function() {
5151

5252
pg.connect(connString, function(err, pgClient, done) {

lib/middlewares/express.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = function(f, profiler) {
2+
if(!f) f = function() { return true; };
3+
return function(req, res, next) {
4+
profiler.configure();
5+
var enabled = f(req, res);
6+
7+
if(req.path.startsWith(profiler.resourcePath)) {
8+
if (!enabled) {
9+
res.setHeader('Content-Type', 'text/plain');
10+
res.writeHead(404);
11+
res.end('MiniProfiler is disabled.');
12+
return;
13+
}
14+
15+
var sp = req.path.split('/');
16+
var reqPath = sp[sp.length - 1];
17+
if(reqPath == 'results')
18+
profiler.results(req, res);
19+
else
20+
profiler.static(reqPath, res);
21+
return;
22+
}
23+
var id = profiler.startProfiling(req, enabled);
24+
25+
res.locals.miniprofiler = enabled ? req.miniprofiler : {
26+
include: function() { return ''; }
27+
};
28+
29+
if (enabled) {
30+
res.on('finish', function() {
31+
profiler.stopProfiling(req);
32+
});
33+
res.setHeader('X-MiniProfiler-Ids', `["${id}"]`);
34+
}
35+
next();
36+
};
37+
};

lib/miniprofiler.js

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,23 @@ _.templateSettings = {
2121
interpolate: /\{(.+?)\}/g
2222
};
2323

24+
var shared = function() {
25+
return {
26+
configure,
27+
startProfiling,
28+
stopProfiling,
29+
results,
30+
static,
31+
resourcePath
32+
};
33+
};
34+
2435
// EXPORTS
2536
exports.configure = configure;
26-
exports.profile = middleware;
37+
exports.express = function(f) {
38+
return require('./middlewares/express.js')(f, shared());
39+
};
40+
2741
exports.for = {
2842
pg: require('./providers/miniprofiler.pg.js'),
2943
redis: require('./providers/miniprofiler.redis.js')
@@ -147,44 +161,6 @@ var includes = {
147161
share: _.template(fs.readFileSync(path.join(includesDir, 'share.html')).toString())
148162
};
149163

150-
function middleware(f) {
151-
if(!f) f = function() { return true; };
152-
return function(req, res, next) {
153-
if(!configured) configure();
154-
var enabled = f(req, res);
155-
156-
if(req.path.startsWith(resourcePath)) {
157-
if (!enabled) {
158-
res.setHeader('Content-Type', 'text/plain');
159-
res.writeHead(404);
160-
res.end('MiniProfiler is disabled.');
161-
return;
162-
}
163-
164-
var sp = req.path.split('/');
165-
var reqPath = sp[sp.length - 1];
166-
if(reqPath == 'results')
167-
results(req, res);
168-
else
169-
static(reqPath, res);
170-
return;
171-
}
172-
var id = startProfiling(req, enabled);
173-
174-
res.locals.miniprofiler = enabled ? req.miniprofiler : {
175-
include: function() { return ''; }
176-
};
177-
178-
if (enabled) {
179-
res.on('finish', function() {
180-
stopProfiling(req);
181-
});
182-
res.setHeader('X-MiniProfiler-Ids', `["${id}"]`);
183-
}
184-
next();
185-
};
186-
}
187-
188164
function include(id) {
189165
// not profiling
190166
if(!id) {

tests/server/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var express = require('express');
33

44
var app = express();
55

6-
app.use(miniprofiler.profile());
6+
app.use(miniprofiler.express());
77

88
app.get('/', (req, res) => {
99
res.send();

tests/server/unprofiled.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var disableMiniProfiler = (req) => {
77
return false;
88
};
99

10-
app.use(miniprofiler.profile(disableMiniProfiler));
10+
app.use(miniprofiler.express(disableMiniProfiler));
1111

1212
app.get('/', (req, res) => {
1313
res.send();

0 commit comments

Comments
 (0)