-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
35 lines (29 loc) · 904 Bytes
/
index.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
var parse = require('url').parse
, send = require('send');
var Server = function(srv, options) {
if (!(this instanceof Server)) return new Server(srv, options);
if (!options) options = {};
if (options.static !== false)
this.serve(srv, "/datachannel.io");
return new exports.Manager(srv, options);
};
Server.prototype.serve = function(srv, path) {
var url = path + '/datachannel.io.js';
var evs = srv.listeners('request').slice(0);
srv.removeAllListeners('request');
srv.on('request', function(req, res) {
if (0 == req.url.indexOf(url)) {
var path = parse(req.url).pathname.split('/').slice(-1)[0].replace('.js','_DEP.js');
send(req, path)
.root(__dirname + '/../client')
.index(false)
.pipe(res);
} else {
for (var i = 0; i < evs.length; i++) {
evs[i].call(srv, req, res);
}
}
});
};
exports.listen = Server;
exports.Manager = require('./manager');