这是一个创建于 3362 天前的主题,其中的信息可能已经有所发展或是发生改变。
/**
* Module dependencies.
*/
var express = require ('express'),
routes = require ('./routes'),
http = require ('http'),
path = require ('path'),
mongoose = require ('mongoose');
var app = express ();
var db =mongoose.createConnection ('mongodb://localhost/test');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var Task = new Schema ({
task:String
},{
collection:'test'
});
var Task = db.model ('test', Task );
// all environments
app.set ('view engine', 'jade');
app.get ('/tasks', function (req, res ){
Task.find ({}, function (err, docs ) {
res.render ('tasks/index', {
title: 'Todos index view',
docs: docs
});
});
});
app.get ('/tasks/new', function (req, res ){
res.render ('tasks/new.jade', {
title: 'New Task'
});
});
app.post ('/tasks',function (req, res ){
var task = new Task (req.body );
console.log (req.body );
task.save (function (err ) {
if (!err ) {
res.redirect ('/tasks');
}
else {
res.redirect ('/tasks/new');
}
});
});
app.listen (3000,function (err ){
if (err ){
console.log (err );
return err;
}
});
console.log ('the project run 3000');