forked from electron-webapps/meteor-electron
-
Notifications
You must be signed in to change notification settings - Fork 3
/
serve.js
80 lines (77 loc) · 2.33 KB
/
serve.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
const EventEmitter = Npm.require('events');
serve = function(path, handler) {
if (Package["iron:router"]){
Package["iron:router"].Router.route(path, function(){
handler(this.request, this.response, this.next);
}, {where: "server"});
} else if (Package["meteorhacks:picker"]){
Package["meteorhacks:picker"].Picker.route(path, function(params, req, res, next){
req.query = params.query;
handler(req, res, next);
});
} else {
WebApp.rawConnectHandlers.use(function(req, res, next){
if (req._parsedUrl.query) {
req.query = Npm.require('querystring').parse(req._parsedUrl.query);
}
if (req._parsedUrl.pathname === path) {
handler(req, res, next);
} else {
next();
}
});
}
};
serveDir = function(dir, handler){
//path starts with dir
if (Package["iron:router"]){
Package["iron:router"].Router.route(dir + "/:stuff", function(){
handler(this.request, this.response, this.next);
}, {where: "server"});
} else if (Package["meteorhacks:picker"]){
Package["meteorhacks:picker"].Picker.route(dir + "/:stuff", function(params, req, res, next){
req.query = params.query;
handler(req, res, next);
});
} else {
var regex = new RegExp("^" + dir);
WebApp.rawConnectHandlers.use(function(req, res, next){
if (regex.test(req.path)) {
handler(req, res, next);
} else {
next();
}
});
}
};
/**
* Sends a HTTP response with the given parameters and ends it.
*
* @global
* @param {Object} response - The HTTP response object.
* @param {number} status - The HTTP status code (200, 206, 400,...).
* @param {Object} [headers=null] - The headers to be sent with the response.
* @param {string|Object} [body=null] - String or readable stream to be sent in the body.
* @returns {Object} null
* @since 0.1.4
* @version 1.0.0
*/
sendResponse = function(response, status, headers, body) {
headers = headers || null;
body = body || null;
if (headers) {
response.writeHead(status, headers);
} else {
response.statusCode = status;
}
if (body instanceof EventEmitter && typeof body.read === 'function') { // readable stream
body.on('open', function() {
body.pipe(response);
});
} else if (_.isString(body)) {
response.end(body);
} else {
response.end();
}
return null;
}