Skip to content

Commit 3e7ac41

Browse files
committed
initial commit
1 parent 8d28064 commit 3e7ac41

16 files changed

+416
-0
lines changed

Jakefile.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var sys = require('sys');
2+
var reporters = require('nodeunit').reporters;
3+
4+
desc('Run applicaiton with full debug information turned on.');
5+
task('debug', [], function () {
6+
console.log('Starting Nodejs Server');
7+
8+
process.env.NODE_ENV = 'debug';
9+
10+
var app = require('./app');
11+
app.listen(3000);
12+
console.log('Server running at http://127.0.0.1:3000/' + ' in debug mode.\r');
13+
});
14+
15+
desc('Run application in release mode with minimal debug information.');
16+
task('release', [], function () {
17+
console.log('Starting Nodejs Server');
18+
19+
process.env.NODE_ENV = 'release';
20+
21+
var app = require('./app');
22+
app.listen(3000);
23+
console.log('Server running at http://127.0.0.1:3000/' + ' in release mode.\r');
24+
});
25+
26+
desc('Run the applications integration tests.');
27+
task('test', [], function () {
28+
console.log('Starting Nodejs');
29+
30+
process.env.NODE_ENV = 'test';
31+
32+
var app = require('./app');
33+
34+
app.listen(3000);
35+
console.log('Running testing server at http://127.0.0.1:3000/' + '\r');
36+
37+
// Delay to make sure that node server has time to start up on slower computers before running the tests.
38+
setTimeout( function(){
39+
require.paths.push(__dirname);
40+
41+
var testRunner = reporters.default;
42+
43+
process.chdir(__dirname);
44+
45+
console.log('Running integration tests.');
46+
testRunner.run(['tests/integration']);
47+
}, 250);
48+
});

app.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var sys = require('sys');
2+
var express = require('express');
3+
var app = module.exports = express.createServer();
4+
5+
var mongoose = require('mongoose');
6+
mongoose.connect('mongodb://localhost/restmvc');
7+
8+
app.configure('debug', function() {
9+
app.use(express.logger({ format: '\x1b[1m :date \x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms\x1b[0m :status' }));
10+
});
11+
12+
app.configure(function() {
13+
app.set('root', __dirname);
14+
app.set('views', __dirname + '/views');
15+
app.set('view engine', 'jade');
16+
app.use(express.favicon());
17+
app.use(express.bodyParser());
18+
app.use(express.methodOverride());
19+
app.use(app.router);
20+
});
21+
22+
// This grabs the index.js file at the root and uses that
23+
var restMvc = require('restmvc.js');
24+
restMvc.Initialize(app, mongoose);
25+
26+
app.error(restMvc.ErrorHandler);
27+
28+
app.use(function(req, res, next){
29+
next(restMvc.RestError.NotFound.create(req.url));
30+
});
31+
32+
// example of how to throw a 404
33+
app.get('/404', function(req, res, next){
34+
next(restMvc.RestError.NotFound.create(req.url));
35+
});
36+
37+
// example of how to throw a 500
38+
app.get('/500', function(req, res, next){
39+
next(new Error('keyboard cat!'));
40+
});
41+
42+
if (!module.parent) {
43+
app.listen(3000);
44+
console.log('Server running at http://127.0.0.1:3000/' + '\r');
45+
}

controllers/person.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports.personController = function(baseController, restMvc){
2+
3+
// this file is not neccessary but is here for demonstration perposes that you can.
4+
// you just need to return the controller, or one that extends the base one.
5+
return baseController;
6+
7+
// Example of how to extend the base controller if you need to...
8+
// var Controller = baseController.extend({
9+
// toString: function(){
10+
// // calls parent "toString" method without arguments
11+
// return this._super(Controller, "toString") + " (Controller)";
12+
// }
13+
// });
14+
};

models/person.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports.person = function (mongoose) {
2+
// Standard Mongoose stuff here...
3+
var schema = mongoose.Schema;
4+
// var objectId = schema.ObjectId;
5+
6+
mongoose.model('Person', new schema({
7+
// _id: objectId,
8+
firstName: String,
9+
lastName: String,
10+
initial: String,
11+
dateOfBirth: Date
12+
}));
13+
14+
return mongoose.model('Person');
15+
};

models/show.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports.show = function (mongoose) {
2+
// Standard Mongoose stuff here...
3+
var schema = mongoose.Schema;
4+
5+
mongoose.model('show', new schema({
6+
textValue: String
7+
}));
8+
9+
return mongoose.model('show');
10+
};

package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "tvherald",
3+
"version": "0.1.1",
4+
"main": "./index.js",
5+
"directories": {"lib": "./lib"},
6+
"engines": {
7+
"node": ">= 0.4.1"
8+
},
9+
"dependencies": {
10+
"restmvc.js": ">= 0",
11+
"jake": "0.1.8",
12+
"nodeunit": "0.5.0",
13+
"express": "2.3.7",
14+
"mongoose": "1.3.6",
15+
"jade": "0.8.5",
16+
"ejs": "0.3.1"
17+
}
18+
}

routes/person.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports.personRoutes = function(personController, app, restMvc){
2+
3+
//Example route implemtation. Uncomment for an example of how to implement a custom route.
4+
// app.get('/people.:format?', function(request, response, next) {
5+
//
6+
// console.log('Overriden list route');
7+
//
8+
// personController.index(function(err, results) {
9+
// if (err) {
10+
// next(new Error('Internal Server Error: see logs for details: ' + err), request, response);
11+
// }
12+
// else {
13+
// if (request.params.format){
14+
// if (request.params.format.toLowerCase() == 'json') {
15+
// response.send(results.map(function(instance) {
16+
// return instance.toObject();
17+
// }));
18+
// }
19+
// else{
20+
// next(restMvc.RestError.BadRequest.create('The \'' + request.params.format + '\' format is not supported at this time.'), request, response);
21+
// }
22+
// }
23+
// else {
24+
// response.render(controller.name, { collection: results.map(function(instance) {
25+
// return instance.toObject();
26+
// })});
27+
// }
28+
// }
29+
// });
30+
// })
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
var createJSON = "{\"firstName\":\"Test\",\"lastName\":\"User\",\"dateOfBirth\": \"10/07/1971\"}";
2+
var updateJSON = "{\"firstName\":\"Test2\",\"lastName\":\"User2\",\"dateOfBirth\": \"10/07/1971\"}";
3+
var newPersonId = '';
4+
5+
var http = require('http');
6+
var TestFixture = require('nodeunit').testCase;
7+
8+
module.exports['HTTP Method'] = TestFixture({
9+
setUp: function (callBack) {
10+
11+
this.localhost = http.createClient(3000, 'localhost');
12+
13+
this.requestHelper = function(request, fn){
14+
request.end();
15+
16+
request.on('response', function (response) {
17+
var responseBody = "";
18+
response.setEncoding('utf8');
19+
20+
response.addListener("data", function(chunk) {
21+
responseBody += chunk;
22+
});
23+
24+
response.on('end', function() {
25+
response.body = responseBody;
26+
fn(response);
27+
});
28+
});
29+
};
30+
31+
callBack();
32+
},
33+
34+
tearDown: function (callBack) {
35+
// clean up
36+
callBack();
37+
},
38+
39+
'POST Should create a new Person' : function(test){
40+
var request = this.localhost.request('POST', '/People.json', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'});
41+
request.write(createJSON);
42+
43+
this.requestHelper(request, function(response){
44+
var actualPerson = JSON.parse(response.body);
45+
var expectedPerson = JSON.parse(createJSON);
46+
47+
newPersonId = actualPerson._id;
48+
//console.log(actualPerson);
49+
50+
test.ok(newPersonId != null);
51+
test.equals(expectedPerson.firstName, actualPerson.firstName);
52+
test.equals(expectedPerson.lastName, actualPerson.lastName);
53+
// test.equals(new Date(expectedPerson.dateOfBirth), new Date(actualPerson.dateOfBirth));
54+
55+
test.equals(response.statusCode, 201);
56+
57+
test.done();
58+
});
59+
},
60+
61+
'GET Should return a single Person when calling /People/{ID}.json' : function(test){
62+
var request = this.localhost.request('GET', '/People/' + newPersonId + '.json', {'Host': 'localhost', 'Accept': 'application/json'});
63+
64+
this.requestHelper(request, function(response){
65+
var actualPerson = JSON.parse(response.body);
66+
var expectedPerson = JSON.parse(createJSON);
67+
68+
test.equals(expectedPerson.firstName, actualPerson.firstName);
69+
test.equals(expectedPerson.lastName, actualPerson.lastName);
70+
// test.equals(expectedPerson.dateOfBirth, actualPerson.dateOfBirth);
71+
72+
test.equals(response.statusCode, 200);
73+
test.done();
74+
});
75+
},
76+
77+
'GET Should get a Bad Request when calling /People/{ID}.xml' : function(test){
78+
var request = this.localhost.request('GET', '/People/' + newPersonId + '.xml', {'Host': 'localhost', 'Accept': 'application/json'});
79+
80+
this.requestHelper(request, function(response){
81+
test.equals(response.statusCode, 400);
82+
test.done();
83+
});
84+
},
85+
86+
'PUT Should update an existing Person' : function(test){
87+
var request = this.localhost.request('PUT', '/People/' + newPersonId + '.json', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'});
88+
request.write(updateJSON);
89+
90+
this.requestHelper(request, function(response){
91+
var actualPerson = JSON.parse(response.body);
92+
var expectedPerson = JSON.parse(updateJSON);
93+
94+
test.equals(expectedPerson.firstName, actualPerson.firstName);
95+
test.equals(expectedPerson.lastName, actualPerson.lastName);
96+
// test.equals(expectedPerson.dateOfBirth, actualPerson.dateOfBirth);
97+
98+
test.equals(response.statusCode, 200);
99+
100+
test.done();
101+
});
102+
},
103+
104+
'PUT Should return 404 when trying to Update Person That Doesn\'t Exist' : function(test){
105+
var request = this.localhost.request('PUT', '/People/XXXXX.json', {'Host': 'localhost', 'Accept': 'application/json', 'Content-Type': 'application/json'});
106+
request.write(updateJSON);
107+
108+
this.requestHelper(request, function(response){
109+
test.ok(response.body.length > 0);
110+
test.equals(response.statusCode, 404);
111+
test.done();
112+
});
113+
},
114+
115+
'GET Should return all people when calling /People.json' : function(test){
116+
var request = this.localhost.request('GET', '/People.json', {'Host': 'localhost', 'Accept': 'application/json'});
117+
118+
this.requestHelper(request, function(response){
119+
test.equals(response.statusCode, 200);
120+
test.ok(response.body.length > 0);
121+
test.done();
122+
});
123+
},
124+
125+
'GET Should return a 404 when calling /People/{ID} with an ID that doesn\'t exist' : function(test){
126+
var request = this.localhost.request('GET', '/People/XXXXX.json', {'Host': 'localhost', 'Accept': 'application/json'});
127+
128+
this.requestHelper(request, function(response){
129+
test.ok(response.body.length > 0);
130+
test.equals(response.statusCode, 404);
131+
test.done();
132+
});
133+
},
134+
135+
'DELETE Should delete person when calling /People/{ID}' : function(test){
136+
var request = this.localhost.request('DELETE', '/People/' + newPersonId, {'Host': 'localhost', 'Accept': 'application/json'});
137+
138+
this.requestHelper(request, function(response){
139+
test.equals(response.statusCode, 200);
140+
test.done();
141+
});
142+
},
143+
144+
'DELETE Should return a 404 when calling /People/{ID} with an ID that doesn\'t exist' : function(test){
145+
var request = this.localhost.request('DELETE', '/People/XXXXX', {'Host': 'localhost', 'Accept': 'application/json'});
146+
147+
this.requestHelper(request, function(response){
148+
test.equals(response.statusCode, 404);
149+
test.done();
150+
});
151+
}
152+
});

views/layout.jade

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
!!! 5
2+
html
3+
head
4+
body!= body

views/person/edit.jade

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
!!! 5
2+
html(lang="en")
3+
head
4+
title="Edit " + person.firstName + ' ' + person.lastName
5+
body
6+
form(method="post", action="/people/" + person._id)
7+
input(type="hidden", name="_method", value="put")
8+
fieldset
9+
legend Name
10+
p
11+
label(for="person[firstName]") First Name:
12+
input(type="text", name="person[firstName]", value=person.firstName)
13+
p
14+
label(for="person[lastName]") Last Name:
15+
input(type="text", name="person[lastName]", value=person.lastName)
16+
p.buttons
17+
input(type="submit", value="Save")

views/person/index.jade

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
!!! 5
2+
html(lang="en")
3+
head
4+
title="List of People"
5+
body
6+
h1 List of People
7+
#container
8+
- each person in people
9+
li= person.firstName + ' ' + person.lastName + ' '
10+
a(href='/people/' + person._id, title='View ' + person.firstName) View |
11+
a(href='/people/' + person._id + '/edit', title='Edit ' + person.firstName) Edit
12+
form(method='post', action='/people/' + person._id)
13+
input(type='hidden', name='_method', value='delete')
14+
input(type='submit', value='delete')
15+
16+
div pager.from

views/person/list.jade

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
!!! 5
2+
html(lang="en")
3+
head
4+
title="List of People"
5+
body
6+
h1 List of People
7+
#container
8+
- each person in people
9+
li= person.firstName + ' ' + person.lastName + ' '
10+
a(href='/people/' + person._id, title='View ' + person.firstName) View |
11+
a(href='/people/' + person._id + '/edit', title='Edit ' + person.firstName) Edit
12+
//link_to({controller: 'person', action: 'edit', id: 123})

0 commit comments

Comments
 (0)