Skip to content

Commit

Permalink
rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
nswbmw committed Sep 6, 2013
0 parents commit 2379269
Show file tree
Hide file tree
Showing 638 changed files with 100,789 additions and 0 deletions.
45 changes: 45 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Module dependencies.
*/

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var MongoStore = require('connect-mongo')(express);
var settings = require('./settings');
var flash = require('connect-flash');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(flash());
app.use(express.favicon(__dirname + '/public/images/favicon.ico'));
app.use(express.logger('dev'));
app.use(express.bodyParser({ keepExtensions: true, uploadDir: './public/images' }));
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret: settings.cookieSecret,
key: settings.db,
cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days
store: new MongoStore({
db: settings.db
})
}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

routes(app);
42 changes: 42 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var mongodb = require('./db');

function Comment(name, day, title, comment) {
this.name = name;
this.day = day;
this.title = title;
this.comment = comment;
}

module.exports = Comment;

//存储一条留言信息
Comment.prototype.save = function(callback) {
var name = this.name,
day = this.day,
title = this.title,
comment = this.comment;
//打开数据库
mongodb.open(function (err, db) {
if (err) {
callback(err);
}
//读取 posts 集合
db.collection('posts', function (err, collection) {
if (err) {
mongodb.close();
callback(err);
}
//通过用户名、时间及标题查找文档,并把一条留言对象添加到该文档的 comments 数组里
collection.update({
"name": name,
"time.day": day,
"title": title
}, {
$push: {"comments": comment}
} , function (err, result) {
mongodb.close();
callback(null);
});
});
});
};
5 changes: 5 additions & 0 deletions models/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var settings = require('../settings'),
Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server;
module.exports = new Db(settings.db, new Server(settings.host, Connection.DEFAULT_PORT, {}));
Loading

0 comments on commit 2379269

Please sign in to comment.