-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathangularonserver.coffee
70 lines (54 loc) · 1.79 KB
/
angularonserver.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
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
jsdom = require './jsdom-with-xmlhttprequest'
createdb = require './createdb'
document = undefined
window = undefined
jsdom = jsdom.jsdom
express = require 'express'
fs = require 'fs'
db = require 'rethinkdb'
delay = (ms, func) -> setTimeout func, ms
interval = (ms, func) -> setInterval func, ms
express = require 'express'
app = express()
app.use express.static('public')
html = fs.readFileSync 'index.html', 'utf8'
app.get '/', (req, res, next) ->
res.end html
products = fs.readFileSync 'public/products.html'
app.get '//products.html', (req, res, next) ->
console.log 'sending //products'
res.end products
getProducts = (req, res) ->
if req.query.type is 'undefined'
db.table('products').run().collect (products) ->
res.end JSON.stringify(products)
else
db.table('products').filter({type: req.query.type}).run().collect (products) ->
res.end JSON.stringify(products)
app.get '//products', (req, res, next) ->
getProducts req, res
app.get '/products', (req, res, next) ->
getProducts req, res
app.get "*", (req, res, next) ->
console.log window.document.location
e = window.document.getElementById 'mainctl'
if window.angular?
scope = window.angular.element(e).scope()
scope.$apply ->
scope.setLocation req.url
return undefined
delay 50, ->
console.log window.document.innerHTML
res.end window.document.innerHTML
else
console.log 'window.angular is not defined'
console.log window.document.innerHTML
process.on 'uncaughtException', (err) ->
console.log 'Uncaught exception:'
console.log err
console.log err.stack
db.connect { host: 'localhost', port: 28015 }, (conn) ->
app.listen 3002
console.log 'Listening on port 3002'
document = jsdom html
window = document.createWindow({localPrefix: 'http://localhost:3002/'})