Permalink
Browse files

change mysql to mongodb;

  • Loading branch information...
1 parent f3b199e commit 30edf8e844a8e7c15e5f3ed3eb69bea3d4a8f7f7 @fengmk2 committed Jun 22, 2012
Showing with 363 additions and 253 deletions.
  1. +2 −0 .gitignore
  2. +0 −18 .gitmodules
  3. +43 −0 .jshintrc
  4. +39 −4 README.md
  5. +22 −0 common/db.js
  6. +13 −25 config.js
  7. +73 −52 controllers/todo.js
  8. +0 −35 lib/csrf.js
  9. +22 −0 lib/patch.js
  10. +28 −3 package.json
  11. +44 −35 server.js
  12. +16 −11 views/error.html
  13. +33 −35 views/index.html
  14. +20 −24 views/layout.html
  15. +8 −11 views/todo/edit.html
View
@@ -0,0 +1,2 @@
+node_modules
+
View
@@ -1,18 +0,0 @@
-[submodule "node_modules/mysql"]
- path = node_modules/mysql
- url = git://github.com/felixge/node-mysql.git
-[submodule "node_modules/connect"]
- path = node_modules/connect
- url = git://github.com/senchalabs/connect.git
-[submodule "node_modules/ejs"]
- path = node_modules/ejs
- url = git://github.com/visionmedia/ejs.git
-[submodule "node_modules/express"]
- path = node_modules/express
- url = git://github.com/visionmedia/express.git
-[submodule "node_modules/mime"]
- path = node_modules/mime
- url = git://github.com/bentomas/node-mime.git
-[submodule "node_modules/qs"]
- path = node_modules/qs
- url = git://github.com/visionmedia/node-querystring.git
View
@@ -0,0 +1,43 @@
+{
+ "predef": [
+ "phantom",
+ "module",
+ "require",
+ "__dirname",
+ "process",
+ "console",
+ "it",
+ "describe",
+ "before",
+ "beforeEach",
+ "after",
+ "should",
+ "rewire",
+ "$"
+ ],
+
+ "browser": true,
+ "node" : true,
+ "es5": true,
+ "bitwise": true,
+ "curly": true,
+ "eqeqeq": true,
+ "forin": false,
+ "immed": true,
+ "latedef": true,
+ "newcap": true,
+ "noarg": true,
+ "noempty": true,
+ "nonew": true,
+ "plusplus": false,
+ "undef": true,
+ "strict": false,
+ "trailing": false,
+ "globalstrict": true,
+ "nonstandard": true,
+ "white": true,
+ "indent": 2,
+ "expr": true,
+ "multistr": true,
+ "onevar": false
+}
View
@@ -6,12 +6,47 @@
## Install
- $ npm install express ejs mysql
+```bash
+$ npm install
+```
## Run
-
- $ node todo/server.js
+
+```bash
+$ node server.js
+```
## Demo
-[http://s8.hk:3888](http://s8.hk:3888)
+[http://todo.cnodejs.net](todo.cnodejs.net)
+
+## Test
+
+```bash
+$ make test
+```
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011, 2012 fengmk2 <fengmk2@gmail.com>.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,22 @@
+/*!
+ * todo - common/db.js
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var mongoskin = require('mongoskin');
+var config = require('../config');
+
+var noop = function () {};
+
+var db = mongoskin.db(config.db);
+db.bind('todo');
+db.todo.ensureIndex({ finished: 1 }, noop);
+
+module.exports = db;
View
@@ -1,28 +1,16 @@
+/*!
+ * todo - config.js
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
-exports.port = 8080;
-exports.email='fengmk2@gmail.com';
-exports.site_name = 'Node TODO';
-exports.site_desc = '';
-exports.session_secret = 'tsoedsosisession_secretonsheecfrxedta';
+"use strict";
-var db_options = {
- host: 'localhost',
- port: 3306,
- user: 'root',
- password: '123456',
- database: 'todo'
-};
+exports.debug = true;
+exports.port = 3000;
+exports.email = 'fengmk2@gmail.com';
+exports.site_name = 'Node TODO';
+exports.site_desc = 'Very simple todo, demo for connect web dev.';
+exports.session_secret = 'todo session secret';
-var mysql = new require('mysql'), db = null;
-if(mysql.createClient) {
- db = mysql.createClient(db_options);
-} else {
- db = new mysql.Client(db_options);
- db.connect(function(err) {
- if(err) {
- console.error('connect db ' + db.host + ' error: ' + err);
- process.exit();
- }
- });
-}
-exports.db = db;
+exports.db = 'mongodb://127.0.0.1/todo_dev';
View
@@ -1,69 +1,90 @@
+/*!
+ * todo - controllers/todo.js
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
-var config = require('../config')
- , db = config.db;
+"use strict";
-exports.index = function(req, res, next) {
- db.query('select * from todo order by finished asc, id asc limit 50', function(err, rows) {
- if(err) return next(err);
- res.render('index', {todos: rows});
- });
-};
+/**
+ * Module dependencies.
+ */
+
+var config = require('../config');
+var db = require('../common/db');
-exports.new = function(req, res, next) {
- var title = req.body.title || '';
- title = title.trim();
- if(!title) {
- return res.render('error', {message: '标题是必须的'});
+exports.index = function (req, res, next) {
+ db.todo.findItems({}, { sort: {_id: 1, finished: 1}}, function (err, rows) {
+ if (err) {
+ return next(err);
}
- db.query('insert into todo set title=?, post_date=now()', [title], function(err, result) {
- if(err) return next(err);
- res.redirect('/');
- });
+ res.render('index.html', {todos: rows});
+ });
};
-exports.view = function(req, res, next) {
+exports.new = function (req, res, next) {
+ var title = req.body.title || '';
+ title = title.trim();
+ if (!title) {
+ return res.render('error.html', {message: '标题是必须的'});
+ }
+ db.todo.save({title: title, post_date: new Date()}, function (err, row) {
+ if (err) {
+ return next(err);
+ }
res.redirect('/');
+ });
};
-exports.edit = function(req, res, next) {
- var id = req.params.id;
- db.query('select * from todo where id=?', [id], function(err, rows) {
- if(err) return next(err);
- if(rows && rows.length > 0) {
- var row = rows[0];
- res.render('todo/edit', {todo: row});
- } else {
- next();
- }
- });
+exports.view = function (req, res, next) {
+ res.redirect('/');
+};
+
+exports.edit = function (req, res, next) {
+ var id = req.params.id;
+ db.todo.findById(id, function (err, row) {
+ if (err) {
+ return next(err);
+ }
+ if (!row) {
+ return next();
+ }
+ res.render('todo/edit.html', {todo: row});
+ });
};
-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', {message: '标题是必须的'});
+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.todo.updateById(id, {$set: {title: title}}, function (err, result) {
+ if (err) {
+ return next(err);
}
- db.query('update todo set title=? where id=?', [title, id], function(err, result) {
- if(err) return next(err);
- res.redirect('/');
- });
+ res.redirect('/');
+ });
};
-exports.delete = function(req, res, next) {
- var id = req.params.id;
- db.query('delete from todo where id = ?', [id], function(err) {
- if(err) return next(err);
- res.redirect('/');
- });
+exports.delete = function (req, res, next) {
+ var id = req.params.id;
+ db.todo.removeById(id, function (err) {
+ 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.query('update todo set finished=? where id=?', [finished, id], 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.todo.updateById(id, {$set: {finished: finished}}, function (err, result) {
+ if (err) {
+ return next(err);
+ }
+ res.redirect('/');
+ });
};
View
@@ -1,35 +0,0 @@
-/**
- * app.dynamicHelpers({
- * csrf: csrf.token
- * });
- *
- * Add csrf parameter to view (ejs example):
- * <form>
- * <input type="hidden" name="csrf" value="<%- csrf %>" />
- * </form>
- */
-exports.token = function(req, res) {
- if(req.session.csrf == null) {
- req.session.csrf = '' + new Date().getTime() + Math.random();
- }
- return req.session.csrf;
-};
-
-/**
- * Express/Connect middleware function for checking csrf token. Usage:
- *
- * app.use(csrf.check());
- */
-exports.check = function() {
- return function(req, res, next) {
- // If request is ajax, no need to check csrf.
- if (!req.xhr && req.body && req.method.toLowerCase() === 'post') {
- var session_csrf = req.session.csrf;
- req.session.csrf = null;
- if (req.body.csrf !== session_csrf) {
- return res.send("Cross-site request forgery attempt discovered!", 403);
- }
- }
- return next();
- };
-};
View
@@ -0,0 +1,22 @@
+/*!
+ * todo - lib/patch.js
+ * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com>
+ * MIT Licensed
+ */
+
+"use strict";
+
+/**
+ * Module dependencies.
+ */
+
+var http = require('http');
+
+if (typeof http.ServerResponse.prototype.redirect === 'undefined') {
+ http.ServerResponse.prototype.redirect = function (url, status) {
+ this.writeHead(status || 302, {
+ Location: url
+ });
+ this.end();
+ };
+}
Oops, something went wrong.

0 comments on commit 30edf8e

Please sign in to comment.