Last active
October 13, 2015 11:48
-
-
Save runvnc/4191057 to your computer and use it in GitHub Desktop.
starting point for node.js app using mongodb and nowjs (not tested)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
I came up with my own way that makes it as straightforward as possible to do 'databasing'. It does use CoffeeScript which I also recommend because it makes things easier also. If you really want to you can convert this to javascript on js2coffee.org. It will look very similar, with more punctuation for the most part. | |
It uses MongolianDeadBeef (mongolian) and NowJS (see nowjs.org, sudo npm install -g now). | |
You will need to specify express 2.x in the package.json to use it as is | |
Server: CoffeeScript (node.js) | |
### | |
express = require 'express' #web server | |
MongolianDeadBeef = require 'mongolian' #database thing | |
server = new MongolianDeadBeef #database thing | |
nowjs = require 'now' #remote procedure call, easier than GET/POST | |
fs = require 'fs' | |
util = require 'util' | |
db = server.db 'appdb' #database thing, appdb is name of db | |
app = express.createServer() #webserver stuff | |
app.use express.cookieParser() #webserver stuff | |
app.use express.session { secret: 'burrito13' } #webserver stuff | |
app.use express.static(__dirname + '/public') #webserver stuff | |
app.get "/", (req, res) -> #webserver stuff | |
res.end fs.readFileSync 'index.html' | |
nowjs = require 'now' #set up rpc stuff see nowjs.org | |
everyone = nowjs.initialize app | |
everyone.now.dbinsert = (col, data) -> #you can call these directly from js code in browser | |
db.collection(col).insert data #database thing | |
everyone.now.dbupdate = (col, criteria, data) -> | |
db.collection(col).update criteria, data #database thing | |
everyone.now.dbfind = (col, callback) -> #nowjs rpc call | |
db.collection(col).find().toArray (err, data) -> #database thing | |
callback data | |
app.listen 3000 #start webserver | |
###____________________________________________________________________________________ | |
Client: (CoffeeScript, convert to JS with coffee -c main.coffee, include along with jquery and nowjs in head of html doc) | |
### | |
$ -> | |
now.ready -> | |
now.dbfind 'things', (things) -> | |
console.log things |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment