summaryrefslogtreecommitdiffstats
path: root/examples/nodejs-mongodb-mongoose-restify/README.md
blob: bf46ba6e14f3f00c995bc4a0d29b62cbca8eef83 (plain)
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
75
76
77
# Simple example - Node.js, Restify, MongoDb and Mongoose

There are basically two big parts to this demo - two servers. The first server, httpServer, serves up static html/js/css to the 
browser and the second, mongodbServer, is purely for saving and retrieving data from the mongodb.  

I've put both servers in the server.js which makes it extremely long and challenging to maintain but it does the job for this demo. Also, each server is listening on its own port. If you required only one public port, you could choose one server to listen on that port then pass the events to the other port which may be interested in different routes.  

In addition to server.js, I've refactored it into two separate files: server-http.js and server-mongo.js. 

## HTTP SERVER

Originally, this tutorial started out as purely a mongodb one but I wanted to see the data in a browser and since this is a collection of Backbone tutorials, I might as well include some client-side backbone views. I aslo started working on it before discovering Google's Yeoman which includes its own web server that serves static files thus making the HTTP portion not necessary when testing locally, however, when you move to host these files somewhere else like nodejitsu, you may need to use your own static web server if it doesn't support nginx or apache.    

To view the data in your browser, you will need to host the app locally. In my case, I started up Yeoman using the terminal. 

$ yeoman server

By default, yeoman looks at the "app" directory. If you update to Yeoman 1.0, you are able to configure this path in the grunt.js file and configure it to look any folder like "public" but at the time of writing this demo, I'm using Yeoman 0.9.6 so will keep the "app" directory.
 
It automatically launches a browswer window to: 

http://localhost:3501/ 

I should see the index.html which presents MainView and its MessagesCollection will then go out and talk to:  

http://localhost:8080/messages 

This static server is taken very largely from this example: 
http://thecodinghumanist.com/blog/archives/2011/5/6/serving-static-files-from-node-js

## MONGODB

In order to setup my mongodb database, I've taken the following steps:

1. I have two terminal windows open.

2. In the the first one, I've started mongoDB: 
$ mongod

3. In the second, I've started the mongo shell
$ mongo

In the mongo shell, I've created a database called "nationalpark"

> use nationalpark <-- will automatically create and use this new database 

then, I've added a collection called "messages" and inserted a message

var message = { message: "onward, upward", hiker: "rosella"}; 

> db.messages.insert(message); <--- once you use nationalpark, db becomes the link to it

Just to prove you've added a message, you can display all the messages 

> db.messages.find();

Now, I have a database with a collection of messages containing at least one message.

## DATABASE

 If you opened your browser to http://localhost:8080/messages, you might see this cryptic message:
{"message":"Invalid sort() argument. Must be a string or object."}

It means there are no messages in your database but we'll get rid of that message by manually inserting a message.

Once you've inserted a mesage, you can open your brower to: http://localhost:8080/messages
and it should spit out the message   


## CONFIG

if you plan to work with a public github, it is a good idea to protect your production mongodb connection uri 
and put it in a config file which you include in .gitignore so that it doesn't get committed  

var config = require('./config'); // Local congig file to hide creds
db = mongoose.connect(config.mongoose_auth),
Schema = mongoose.Schema;