/
connect-plugin.js
55 lines (47 loc) · 1.51 KB
/
connect-plugin.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
var connect = require("connect");
/**
* In this example all interactions with the plugin system are encaplsulated in
* the startup() function. This makes it easy to test and develop
* the main functionality (createServer) independently.
*/
var exports = module.exports = function startup(options, imports, register) {
imports.log.info("connect plugin start");
var server = exports.createServer();
server.listen(options.port, options.host, function(err) {
if (err)
return register(err);
imports.log.info("Connect server listening at http://" + options.host + ":" + options.port);
register(null, {
"onDestruct": function(callback) {
server.close();
server.on("close", callback);
},
"connect.route": {
addRoute: server.addRoute
},
"http": {
getServer: function() {
return server;
}
}
});
});
};
/**
* The main funcitonality.
*
* It is good practice to make this also usable without the plugin system. In
* bigger plugins this should be extgracted in a separate module
*/
exports.createServer = function(routes) {
var app;
var server = connect()
.use(connect.router(function(app_) {
app = app_;
}));
server.addRoute = function(method, route, handler) {
method = method.toLowerCase() || "get";
app[method](route, handler);
};
return server;
};