TruePattern

Todo Application

Lets say Hello, World to Node.js with a Todo app

Goal

  • To run a Todo Application that can capture Todo’s from user and save it on server (persist)

The application and requirements are well known, so no need to go over what todo is. If you need pointers, please refer to this page.

Deliverables

  • Todo Server running on Node.js, MongoDB exposing REST API
  • Todo MVC client on Backbone.js using REST to connect with server

Todo Server

There are some cool frameworks for Node coming up every day and its a fast changing landscape. To keep things simple, we’ll base our technologies on a robust ground with proven technologies like ExpressJS and MongooseJS. I have written a wrapper over both of these proven technologies (common settings that would help you bootstrap your application quicker) in aonx.

Directory structure

I prefer a very modular approach, where all the models, controllers and views are self contained within a directory (similar to django) and that way it could be potentially reused elsewhere. So we’ll go with a structure

  • apps – contains todo module
  • config – config for todo server
  • public – client side js, css, etc.,
  • test – mocha tests
  • server.js – main server js file

Todo App

Todo model (todo_model.js) download
1
2
3
4
5
var Todo = new Schema({
    content  : { type:String, required:true, index: true },
    done : { type:Boolean, default: false  },
    order  : Number
  });

as you can see, the model part is trivial, similarly aonx provides crudHelpers for mongoose, which make the controller a straight forward express-resource hookup as given below

Todo controller (todo_controller.js) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
exports.index = function(req, res){
  Todo.index(req,res);
};

exports.new = function(req, res){
  Todo.createObject(req, res);
};

exports.create = function(req, res){
  Todo.createObject(req, res);
};

exports.show = function(req, res){
  Todo.showObject(req.todo, req, res);
};

exports.update = function(req, res){
  Todo.updateObject(req.todo, req, res);
};

exports.destroy = function(req, res){
  Todo.removeObject(req.todo, req, res);
};

please refer to express-resource module for the crud routing hookup. As a last step, we’ll bind the controllers to the url of our choice (todos/*)

REST url (todo_url.js) download
1
2
// hookup the controller exported methods to this resource
app.resource('todos', require('./controllers'));

Testing

Now, the part where we want to make sure all the REST part is working fine. Similar to aonx, I have put together a mocha wrapper for crudTesting called berry. Its like just give the url and a sample object, it would verify create, read, update and delete. The sample unit test code is as follows

Unit Test (todo_test.js) download
1
2
3
4
  var url = serverUrl + '/v1/todos';
  var todoObj = {title:'Hello Todo'};
  var todoObj1 = {title:'Updated Hello Todo'};
  var crudfn = berry.crudTester(url, todoObj, todoObj1);

Run the server

Now that you have made it so far, following are the instructions to run the todo server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# make sure node, npm, mongodb are installed
git clone https://github.com/truepattern/todo-server.git
cd todo-server
npm install -d

# lets run unit tests
make

# run the server
node server.js

# go to another terminal
curl localhost:8080/api/v1/todos
curl localhost:8080/api/v1/todos/new?content=hello
curl localhost:8080/api/v1/todos

Todo Client

I have copied the todo mvc client (backbone.js based) from here. Just one change, instead of localstorage, now the client points to the localhost server. You can see the client by going to browser localhost:8080/index.html

Comments

Comments