Skip to content

Commit 5986b9a

Browse files
committed
updates to restify example to add tests and Yeoman support
1 parent 404bea7 commit 5986b9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+11484
-28
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
8+
[*]
9+
10+
# Change these settings to your own preference
11+
indent_style = space
12+
indent_size = 4
13+
14+
# We recommend you to keep these unchanged
15+
end_of_line = lf
16+
charset = utf-8
17+
trim_trailing_whitespace = true
18+
insert_final_newline = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
temp
2+
dist
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"node": true,
3+
"browser": true,
4+
"es5": true,
5+
"esnext": true,
6+
"bitwise": true,
7+
"camelcase": true,
8+
"curly": true,
9+
"eqeqeq": true,
10+
"immed": true,
11+
"indent": 4,
12+
"latedef": true,
13+
"newcap": true,
14+
"noarg": true,
15+
"quotmark": "single",
16+
"regexp": true,
17+
"undef": true,
18+
"unused": true,
19+
"strict": true,
20+
"trailing": true,
21+
"smarttabs": true
22+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
module.exports = function( grunt ) {
2+
'use strict';
3+
//
4+
// Grunt configuration:
5+
//
6+
// https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
7+
//
8+
grunt.initConfig({
9+
10+
// Project configuration
11+
// ---------------------
12+
13+
// specify an alternate install location for Bower
14+
bower: {
15+
dir: 'public/components'
16+
},
17+
18+
// Coffee to JS compilation
19+
coffee: {
20+
compile: {
21+
files: {
22+
'temp/scripts/*.js': 'public/scripts/**/*.coffee'
23+
},
24+
options: {
25+
basePath: 'public/scripts'
26+
}
27+
}
28+
},
29+
30+
// compile .scss/.sass to .css using Compass
31+
compass: {
32+
dist: {
33+
// http://compass-style.org/help/tutorials/configuration-reference/#configuration-properties
34+
options: {
35+
css_dir: 'temp/styles',
36+
sass_dir: 'public/styles',
37+
images_dir: 'public/images',
38+
javascripts_dir: 'temp/scripts',
39+
force: true
40+
}
41+
}
42+
},
43+
44+
// generate application cache manifest
45+
manifest:{
46+
dest: ''
47+
},
48+
49+
// headless testing through PhantomJS
50+
mocha: {
51+
all: ['test/**/*.html']
52+
},
53+
54+
// default watch configuration
55+
watch: {
56+
coffee: {
57+
files: 'public/scripts/**/*.coffee',
58+
tasks: 'coffee reload'
59+
},
60+
compass: {
61+
files: [
62+
'public/styles/**/*.{scss,sass}'
63+
],
64+
tasks: 'compass reload'
65+
},
66+
reload: {
67+
files: [
68+
'public/*.html',
69+
'public/styles/**/*.css',
70+
'public/scripts/**/*.js',
71+
'public/images/**/*'
72+
],
73+
tasks: 'reload'
74+
}
75+
},
76+
77+
// default lint configuration, change this to match your setup:
78+
// https://github.com/cowboy/grunt/blob/master/docs/task_lint.md#lint-built-in-task
79+
lint: {
80+
files: [
81+
'Gruntfile.js',
82+
'public/scripts/**/*.js',
83+
'spec/**/*.js'
84+
]
85+
},
86+
87+
// specifying JSHint options and globals
88+
// https://github.com/cowboy/grunt/blob/master/docs/task_lint.md#specifying-jshint-options-and-globals
89+
jshint: {
90+
options: {
91+
curly: true,
92+
eqeqeq: true,
93+
immed: true,
94+
latedef: true,
95+
newcap: true,
96+
noarg: true,
97+
sub: true,
98+
undef: true,
99+
boss: true,
100+
eqnull: true,
101+
browser: true
102+
},
103+
globals: {
104+
jQuery: true
105+
}
106+
},
107+
108+
// Build configuration
109+
// -------------------
110+
111+
// the staging directory used during the process
112+
staging: 'temp',
113+
// final build output
114+
output: 'dist',
115+
116+
mkdirs: {
117+
staging: 'public/'
118+
},
119+
120+
// Below, all paths are relative to the staging directory, which is a copy
121+
// of the public/ directory. Any .gitignore, .ignore and .buildignore file
122+
// that might appear in the public/ tree are used to ignore these values
123+
// during the copy process.
124+
125+
// concat css/**/*.css files, inline @import, output a single minified css
126+
css: {
127+
'styles/main.css': ['styles/**/*.css']
128+
},
129+
130+
// renames JS/CSS to prepend a hash of their contents for easier
131+
// versioning
132+
rev: {
133+
js: 'scripts/**/*.js',
134+
css: 'styles/**/*.css',
135+
img: 'images/**'
136+
},
137+
138+
// usemin handler should point to the file containing
139+
// the usemin blocks to be parsed
140+
'usemin-handler': {
141+
html: 'index.html'
142+
},
143+
144+
// update references in HTML/CSS to revved files
145+
usemin: {
146+
html: ['**/*.html'],
147+
css: ['**/*.css']
148+
},
149+
150+
// HTML minification
151+
html: {
152+
files: ['**/*.html']
153+
},
154+
155+
// Optimizes JPGs and PNGs (with jpegtran & optipng)
156+
img: {
157+
dist: '<config:rev.img>'
158+
},
159+
160+
// rjs configuration. You don't necessarily need to specify the typical
161+
// `path` configuration, the rjs task will parse these values from your
162+
// main module, using http://requirejs.org/docs/optimization.html#mainConfigFile
163+
//
164+
// name / out / mainConfig file should be used. You can let it blank if
165+
// you're using usemin-handler to parse rjs config from markup (default
166+
// setup)
167+
rjs: {
168+
// no minification, is done by the min task
169+
optimize: 'none',
170+
baseUrl: './scripts',
171+
wrap: true,
172+
name: 'main'
173+
},
174+
175+
// While Yeoman handles concat/min when using
176+
// usemin blocks, you can still use them manually
177+
concat: {
178+
dist: ''
179+
},
180+
181+
min: {
182+
dist: ''
183+
}
184+
});
185+
186+
// Alias the `test` task to run the `mocha` task instead
187+
grunt.registerTask('test', 'server:phantom mocha');
188+
189+
};
Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,77 @@
1-
Message me if you need a hand getting this to work
1+
# Simple example - Node.js, Restify, MongoDb and Mongoose
2+
3+
There are basically two big parts to this demo - two servers. The first server, httpServer, serves up static html/js/css to the
4+
browser and the second, mongodbServer, is purely for saving and retrieving data from the mongodb.
5+
6+
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.
7+
8+
In addition to server.js, I've refactored it into two separate files: server-http.js and server-mongo.js.
9+
10+
## HTTP SERVER
11+
12+
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.
13+
14+
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.
15+
16+
$ yeoman server
17+
18+
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.
19+
20+
It automatically launches a browswer window to:
21+
22+
http://localhost:3501/
23+
24+
I should see the index.html which presents MainView and its MessagesCollection will then go out and talk to:
25+
26+
http://localhost:8080/messages
27+
28+
This static server is taken very largely from this example:
29+
http://thecodinghumanist.com/blog/archives/2011/5/6/serving-static-files-from-node-js
30+
31+
## MONGODB
32+
33+
In order to setup my mongodb database, I've taken the following steps:
34+
35+
1. I have two terminal windows open.
36+
37+
2. In the the first one, I've started mongoDB:
38+
$ mongod
39+
40+
3. In the second, I've started the mongo shell
41+
$ mongo
42+
43+
In the mongo shell, I've created a database called "nationalpark"
44+
45+
> use nationalpark <-- will automatically create and use this new database
46+
47+
then, I've added a collection called "messages" and inserted a message
48+
49+
var message = { message: "onward, upward", hiker: "rosella"};
50+
51+
> db.messages.insert(message); <--- once you use nationalpark, db becomes the link to it
52+
53+
Just to prove you've added a message, you can display all the messages
54+
55+
> db.messages.find();
56+
57+
Now, I have a database with a collection of messages containing at least one message.
58+
59+
## DATABASE
60+
61+
If you opened your browser to http://localhost:8080/messages, you might see this cryptic message:
62+
{"message":"Invalid sort() argument. Must be a string or object."}
63+
64+
It means there are no messages in your database but we'll get rid of that message by manually inserting a message.
65+
66+
Once you've inserted a mesage, you can open your brower to: http://localhost:8080/messages
67+
and it should spit out the message
68+
69+
70+
## CONFIG
71+
72+
if you plan to work with a public github, it is a good idea to protect your production mongodb connection uri
73+
and put it in a config file which you include in .gitignore so that it doesn't get committed
74+
75+
var config = require('./config'); // Local congig file to hide creds
76+
db = mongoose.connect(config.mongoose_auth),
77+
Schema = mongoose.Schema;

0 commit comments

Comments
 (0)