这是suqian大牛的Simple-TODO Nodejs 实现版 :http://cnodejs.org/topic/4f16442ccae1f4aa270010d5
最近他把数据库改用MongoDB了,部署的方法可以参考这里:http://blog.csdn.net/kunshan_shenbin/article/details/7725415
由于近阶段一直在研究CouchDB,所以也弄了个couchdb的实现,代码完全基于上面提到的Simple-TODO Nodejs实现。
步骤如下:
1. 下载Simple-TODO Nodejs实现版至本地。
git clone https://github.com/fengmk2/todo.git
2. 安装couchdb的驱动插件cradle
进入todo目录后运行 npm install cradle
3. 只需对代码稍作修改即可。
config.js中追加如下3个设置项:
exports.db_port = 5984; exports.db_url = 'http://localhost'; exports.db_name = 'todo_dev';
"use strict"; /** * Module dependencies. */ var cradle = require('cradle'); var config = require('../config'); var connection = new(cradle.Connection)(config.db_port, config.db_port, { cache: true, raw: false }); var db = connection.database(config.db_name); db.exists(function (err, exists) { if (err) { console.log('error', err); } else if (exists) { console.log('database exists, please enjoy.'); } else { console.log('database does not exist, create a new one.'); db.create(function() { db.save('_design/todo', { views: { all: { map: 'function (doc) { emit(doc._id, doc) }' } } }); }); } }); module.exports = db;
"use strict"; /** * Module dependencies. */ var config = require('../config'); var db = require('../common/db'); exports.index = function (req, res, next) { db.view('todo/all', function (err, result) { if (err) { return next(err); } var data = new Array(); result.forEach(function (row) { data.push(row); }); res.render('index.html', {todos: data}); }); }; exports.new = function (req, res, next) { var title = req.body.title || ''; title = title.trim(); if (!title) { return res.render('error.html', {message: '标题是必须的'}); } db.save({title: title, post_date: new Date()}, function (err, result) { if (err) { return next(err); } res.redirect('/'); }); }; exports.view = function (req, res, next) { res.redirect('/'); }; exports.edit = function (req, res, next) { var id = req.params.id; db.get(id, function (err, doc) { if (err) { return next(err); } if (!doc) { return next(); } res.render('todo/edit.html', {todo: doc}); }); }; exports.save = function (req, res, next) { var id = req.params.id; var title = req.body.title || ''; title = title.trim(); if (!title) { return res.render('error.html', {message: '标题是必须的'}); } db.merge(id, {title: title}, function (err, result) { if (err) { return next(err); } res.redirect('/'); }); }; exports.delete = function (req, res, next) { var id = req.params.id; db.get(id, function (err, doc) { var rev = doc._rev; db.remove(id, rev, function (err, result) { if (err) { return next(err); } res.redirect('/'); }); }); }; exports.finish = function (req, res, next) { var finished = req.query.status === 'yes' ? 1 : 0; var id = req.params.id; db.merge(id, {finished: finished}, function (err, result) { if (err) { return next(err); } res.redirect('/'); }); };