forked from socketstream/socketstream
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.coffee
39 lines (32 loc) · 1.13 KB
/
router.coffee
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
# HTTP Router
# -----------
# Right now the router is simply an EventEmitter. This may change in the future
EventEmitter2 = require('eventemitter2').EventEmitter2
class exports.Router
constructor: ->
@ee = new EventEmitter2
wildcard: true
delimiter: '?'
# Try the original route first for speed. If none exists, recursively fall back until we find a route, if possible
# This allows us to fully support HTML5 pushState 'mock routing' across multiple single-page clients
route: (url, req, res) ->
# TODO allow for widcards with listeners = @ee.listeners(url) - @ee.listenersAny(url)
if @ee.listeners(url).length > 0
@ee.emit(url, req, res)
true
else
if url == '/'
return false
if url.indexOf('?') >= 0
sr = url.split('?')
else
sr = url.split('/')
sr.pop()
newUrl = sr.join('/')
newUrl = '/' unless newUrl.length > 0
@route(newUrl, req, res)
on: (url, cb) ->
if url.substring(0,1) == '/' && url.indexOf(' ') == -1
@ee.on(url, cb)
else
throw new Error(url + ' is not a valid URL. Valid URLs must start with /')