Migrate the simple-Todo nodejs implementation version to the couchdb Database

Source: Internet
Author: User
Tags couchdb

This is Suqian Daniel's simple-Todo nodejs implementation version: http://cnodejs.org/topic/4f16442ccae1f4aa270010d5

Recently he switched the database to MongoDB, the deployment method can refer to here: http://blog.csdn.net/kunshan_shenbin/article/details/7725415

Since couchdb has been being studied in the recent stage, we have also implemented couchdb. The Code is fully implemented based on the simple-Todo nodejs mentioned above.

The procedure is as follows:

1. Download the simple-Todo nodejs implementation version to your local device.

Git clone https://github.com/fengmk2/todo.git

2. Install the couchdb driver plug-in cradle

Go to the todo directory and run NPM install cradle.

3. You only need to slightly modify the code.

Add the following three settings to config. JS:

exports.db_port = 5984;exports.db_url = 'http://localhost';exports.db_name = 'todo_dev';

Modify common/DB. js as follows:

"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;

Controllers/todo. js needs to be changed to the couchdb operation through cradle in the Database Operations Section.

"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) returns data.push(row={}}res.render('index.html ', {todos: Data}) ;}; exports. new = function (req, res, next) {var Title = req. bo Dy. Title | ''; Title = title. Trim (); If (! Title) {return res.render('error.html ', {message: 'title is required'});} 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: 'title is required'});} 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 ('/') ;}) ;};}; ex Ports. 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 ('/');});};

After that, start the operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.