这是一个创建于 3378 天前的主题,其中的信息可能已经有所发展或是发生改变。
/**
* Module dependencies.
*/
var express = require ('express'),
routes = require ('./routes'),
http = require ('http'),
path = require ('path'),
mongoose = require ('mongoose');
var bodyParser = require ('body-parser')
var app = express ();
var db = mongoose.createConnection ('mongodb://localhost/test');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var Task = new Schema ({
task: {type:String}
});
var Task = db.model ('Task', Task );
// all environments
app.set ('view engine', 'jade');
// parse application/x-www-form-urlencoded
app.use (bodyParser.urlencoded ({ extended: false }))
// parse application/json
app.use (bodyParser.json ())
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.task );
task.save (function (err ) {
if (!err ) {
res.redirect ('/tasks');
}
else {
res.redirect ('/tasks/new');
}
console.log (req.body );
});
});
app.listen (3000,function (err ){
if (err ){
console.log (err );
return err;
}
});
console.log ('the project run 3000');