This repository was archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.coffee
74 lines (64 loc) · 1.91 KB
/
main.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
71
72
73
74
Db = require('mongodb').Db
Connection = require('mongodb').Connection
Server = require('mongodb').Server
Mongo = require('mongodb')
express = require('express')
jsdom = require('jsdom')
fs = require('fs')
path = require('path')
require('./coffee/handlers.js')
global.db = new Db 'dbs', new Server('127.0.0.1', 34006, {})
app = express.createServer()
app.configure () ->
app.set('view_engine', 'jade')
app.set('layout')
# app.use allowCrossDomain
app.use '/public', express.static(__dirname + '/public')
app.use express.bodyParser()
# app.get '/', dbs.indexHandler
app.get '/', (req, res) ->
# locals = message: "Express + Haml working", pageTitle: "Express and Jade", youAreUsingJade: true
# res.render 'index.jade', {locals: locals, layout: null}
db.open (err, db) ->
if err
console.log err + "no open err"
return null
items = []
db.collection 'data', (err, collection) ->
collection.find {}, {}, (err, cursor) ->
if err
console.log "Error in collection find" + err
cursor.toArray (err, items) ->
if err
console.log "Error in iteration" + err
res.render 'index.jade', {locals: {items: items}, layout: null}
# db.close()
app.get '/getJson', (req, res) ->
db.open (err, db) ->
if err
console.log err + "no open err"
return null
items = []
db.collection 'data', (err, collection) ->
collection.find {}, {}, (err, cursor) ->
if err
console.log "Error in collection find" + err
cursor.toArray (err, items) ->
if err
console.log "Error in iteration" + err
res.write JSON.stringify(items)
res.end()
app.get '/login', (req, res) ->
res.send "login page"
app.post '/data', (req, res) ->
res.header 'Access-Control-Allow-Origin', '*'
data = req.body['data']
db.open (err, db) ->
if err is true
return null
db.collection 'data', (err, collection) ->
for e in data
console.log e
collection.insert e
db.close()
app.listen 3000