Skip to content

Commit 21fddec

Browse files
committed
updates to readme and further testing against the change to involve yeoman
1 parent 3665deb commit 21fddec

File tree

7 files changed

+76
-31
lines changed

7 files changed

+76
-31
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!./config.js

examples/nodejs-mongodb-mongoose-restify/README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
# Simple example - Node.js, Restify, MongoDb and Mongoose
22

3-
original author: Thomas Davis | https://github.com/thomasdavis
4-
editor/hi-jacker: Brandon Flowers | https://github.com/headwinds
3+
authors: Thomas Davis | https://github.com/thomasdavis
4+
Brandon Flowers | https://github.com/headwinds
55

6-
There are basically two big parts to this demo - two servers. The first server, httpServer, serves up static html/js/css to the
7-
browser and the second, mongodbServer, is purely for saving and retrieving data from the mongodb.
6+
If you would like to discuss any of this code, please your leave comments using disqus at the bottom of this article:
7+
8+
http://backbonetutorials.com/nodejs-restify-mongodb-mongoose/
9+
10+
## STRUCTURE
11+
12+
There are basically two parts to this demo - two servers - within one file, server.js, which may sound a little confusing but the two servers do different things.
13+
14+
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.
815

916
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 are only allowed access to one public port, you could choose one server to listen on that port then pass the events to the other server which may be interested in different routes. For instance, in this case, if the http server listens for a /messages route, it could trigger an event and pass that to the mongo server.
1017

11-
In addition to server.js, I've also refactored it into two separate files: server-http.js and server-mongo.js.
18+
In addition to server.js, I've also refactored it into two separate files: server-http.js and server-mongo.js.
19+
20+
If you only need the mongobd server, you might start with the server-mongo folder.
1221

1322
## HTTP SERVER
1423

15-
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.
24+
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.
25+
26+
But before using Yeoman, you might want to try open a terminal to the directory of this app and typing:
1627

17-
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.
28+
$ node server
29+
30+
Next, you will need to open browser and point it to:
31+
32+
http://localhost:8080/
33+
34+
Alternatively, you can use Yeoman which would automatically launch a browswer window and also you more features like live reload.
1835

1936
$ yeoman server
2037

@@ -24,9 +41,8 @@ Yeoman automatically launches a browser window to:
2441

2542
http://localhost:3501/
2643

27-
You can also see this same view by visiting the redundant http server:
44+
http server:
2845

29-
http://localhost:8080/
3046

3147
If you'd like to see the raw messages as a json dump, you can point your browser to:
3248

examples/nodejs-mongodb-mongoose-restify/app/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ <h1>Simple example - Node.js, Restify, MongoDb and Mongoose</h1>
2323
</p>
2424
<p>Check list</p>
2525
<ol>
26-
<li>Have you installed and started mongod in your terminal window? $ mongod</li>
26+
<li>Have you installed and started mongodb in your terminal window? type: mongod</li>
2727
<li>Have you opened a second terminal window and created a sample message in the messages collection of your local mongodb database?</li>
2828
<li>After creating a sample message, you should start your server.js by typing: node server</li>
29-
<li>Finally, open yet another terminal window and type: yeoman server</li>
3029
</ol>
3130
<h2>Messages</h2>
3231
</div>
@@ -36,6 +35,7 @@ <h2>Messages</h2>
3635
<div class="resources">
3736
<p>
3837
If you start to see your messages build up in the Message box above, congratulations! You've successfully setup this simple demo.
38+
</p>
3939
</div>
4040

4141
</body>

examples/nodejs-mongodb-mongoose-restify/server-http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ var getFilePath = function(url) {
4747

4848
console.log("url: " + url);
4949

50-
var filePath = './public' + url;
50+
var filePath = './app' + url;
5151

52-
if (url == '/' ) filePath = './public/index.html';
52+
if (url == '/' ) filePath = './app/index.html';
5353

5454
console.log("filePath: " + filePath);
5555

examples/nodejs-mongodb-mongoose-restify/server-mongo/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
In order to persist data to a mongodb database, a server is created to handle all the CRUD operations.
44

5-
This demo attempts to answer two burning questions:
6-
7-
1. how do I setup a local server to test mongodb?
8-
2. how do I deploy this server to the internet and use it to store date for my other projects?
9-
105
## LOCAL
116

127
The quickest way to learn mongodb is by installing it on your local machine using a service like brew or mac ports. For instance, to install mongodb using brew open a terminal window:

examples/nodejs-mongodb-mongoose-restify/server-mongo/server-mongo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mongodbServer.listen(mongodbPort, function() {
8484
var consoleMessage = '\n MongoDb, Mongoose, Restify, and Backbone Tutorial'
8585
consoleMessage += '\n +++++++++++++++++++++++++++++++++++++++++++++++++++++'
8686
consoleMessage += '\n\n %s your mongodbServer is listening at %s';
87-
consoleMessage += '\n\n open your browser to http://localhost:8888 \n\n';
87+
consoleMessage += '\n\n open your browser to http://localhost:8888/messages \n\n';
8888
consoleMessage += '+++++++++++++++++++++++++++++++++++++++++++++++++++++ \n\n'
8989

9090
console.log(consoleMessage, mongodbServer.name, mongodbServer.url);

examples/nodejs-mongodb-mongoose-restify/server.js

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ var sendHTML = function( filePath, contentType, response ){
5050

5151
var getFilePath = function(url) {
5252

53-
var filePath = './public' + url;
54-
if (url == '/' ) filePath = './public/index.html';
53+
var filePath = './app' + url;
54+
if (url == '/' ) filePath = './app/index.html';
5555

5656
console.log("url: " + url)
5757

@@ -135,28 +135,59 @@ var MessageSchema = new Schema({
135135

136136
// Use the schema to register a model
137137
mongoose.model('Message', MessageSchema);
138-
var Message = mongoose.model('Message');
138+
var MessageMongooseModel = mongoose.model('Message'); // just to emphasize this isn't a Backbone Model
139+
140+
141+
/*
142+
143+
this approach was recommended to remove the CORS restrictions instead of adding them to each request
144+
but its not working right now?! Something is wrong with adding it to mongodbServer
145+
146+
// Enable CORS
147+
mongodbServer.all( '/*', function( req, res, next ) {
148+
res.header( 'Access-Control-Allow-Origin', '*' );
149+
res.header( 'Access-Control-Allow-Method', 'POST, GET, PUT, DELETE, OPTIONS' );
150+
res.header( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-File-Name, Content-Type, Cache-Control' );
151+
if( 'OPTIONS' == req.method ) {
152+
res.send( 203, 'OK' );
153+
}
154+
next();
155+
});
156+
157+
158+
*/
139159

140160

141161
// This function is responsible for returning all entries for the Message model
142162
var getMessages = function(req, res, next) {
143163
// Resitify currently has a bug which doesn't allow you to set default headers
144164
// This headers comply with CORS and allow us to mongodbServer our response to any origin
145-
res.header("Access-Control-Allow-Origin", "*");
146-
res.header("Access-Control-Allow-Headers", "X-Requested-With");
165+
res.header( 'Access-Control-Allow-Origin', '*' );
166+
res.header( 'Access-Control-Allow-Method', 'POST, GET, PUT, DELETE, OPTIONS' );
167+
res.header( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-File-Name, Content-Type, Cache-Control' );
168+
169+
if( 'OPTIONS' == req.method ) {
170+
res.send( 203, 'OK' );
171+
}
147172

148173
console.log("mongodbServer getMessages");
149174

150-
Message.find().limit(20).sort('date', -1).execFind(function (arr,data) {
175+
MessageMongooseModel.find().limit(20).sort('date', -1).execFind(function (arr,data) {
151176
res.send(data);
152177
});
153178
}
154179

155180
var postMessage = function(req, res, next) {
156-
res.header("Access-Control-Allow-Origin", "*");
157-
res.header("Access-Control-Allow-Headers", "X-Requested-With");
181+
res.header( 'Access-Control-Allow-Origin', '*' );
182+
res.header( 'Access-Control-Allow-Method', 'POST, GET, PUT, DELETE, OPTIONS' );
183+
res.header( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-File-Name, Content-Type, Cache-Control' );
184+
185+
if( 'OPTIONS' == req.method ) {
186+
res.send( 203, 'OK' );
187+
}
188+
158189
// Create a new message model, fill it up and save it to Mongodb
159-
var message = new Message();
190+
var message = new MessageMongooseModel();
160191

161192
console.log("mongodbServer postMessage: " + req.params.message);
162193

@@ -172,9 +203,9 @@ mongodbServer.listen(mongodbPort, function() {
172203
var consoleMessage = '\n A Simple MongoDb, Mongoose, Restify, and Backbone Tutorial'
173204
consoleMessage += '\n +++++++++++++++++++++++++++++++++++++++++++++++++++++'
174205
consoleMessage += '\n\n %s says your mongodbServer is listening at %s';
175-
consoleMessage += '\n great! now open your browser to http://localhost:3501';
176-
consoleMessage += '\n where you will connect to your httpServer that will';
177-
consoleMessage += '\n talk to your mongodbServer to get and post your messages. \n\n';
206+
consoleMessage += '\n great! now open your browser to http://localhost:8080';
207+
consoleMessage += '\n it will connect to your httpServer to get your static files';
208+
consoleMessage += '\n and talk to your mongodbServer to get and post your messages. \n\n';
178209
consoleMessage += '+++++++++++++++++++++++++++++++++++++++++++++++++++++ \n\n'
179210

180211
console.log(consoleMessage, mongodbServer.name, mongodbServer.url);
@@ -184,3 +215,5 @@ mongodbServer.listen(mongodbPort, function() {
184215
mongodbServer.get('/messages', getMessages);
185216
mongodbServer.post('/messages', postMessage);
186217

218+
219+

0 commit comments

Comments
 (0)