-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (71 loc) · 1.96 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var express = require('express');
var fs = require('fs');
var mongoose = require('mongoose');
var Schema = mongoose.Schema,
ObjectId = mongoose.ObjectId;
mongoose.connect('mongodb://127.0.0.1:27017/blog');
var blogPost = new Schema({
title: ObjectId,
body: String,
date: Date
});
var app = module.exports = express.createServer();
app.configure(function(){
app.set('views',__dirname + '/views');
app.set('view engine','jade');
app.register('.html',require('jade'));
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use('/public',express.static(__dirname + '/public'));
//app.use(express.router);
});
app.configure('development',function(){
//app.use(express.errorHandler({dumpException:true,'showStack':true}));
//log('Warning:Server in Development Mode, add NODE_ENV=production',true);
});
app.configure('production',function(){
//app.use(express.errorHandler());
//log('Production Mode');
});
app.set('view options',{layout:false});
//var routes = JSON.parse(fs.readFileSync('router.json','utf-8').substr(1));
/*
var startRouter = function(path){
app.get(route, function(req,res){
//console.log('connect to ' + path);
var page = info[routes[path].data];
res.render(routes[path].template.page);
});
};
*/
//for(route in routes){
// startRouter(route);
//}
var blogPost = mongoose.model('blogPost',blogPost);
var post = new blogPost();
app.get('/',function(req,res){
res.render('index',{title:'this is a test!'});
});
//app.use('/blog',require(./blog.js));
// add blog
app.get('/new',function(req,res){
res.render('new',{title:'add a new blog'});
});
// save blog
app.post('/add',function(req,res){
post.title = req.body.title;
post.body = req.body.content;
post.date = new Date();
post.save(function(err){
if(err){
console.log('save faile!');
}else {
console.log('save success!');
res.render('list',{title:'blog list'});
}
});
});
// blog list
// delete blog
app.listen(3000);
console.log('blog server start!');