forked from caijiehong/microblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (62 loc) · 2.03 KB
/
app.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
var express = require('express')
, http = require('http')
, path = require('path');
var MongoStore = require('connect-mongo');
var settings = require('./Settings.js');
var controllers = {};
var app = express();
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.logger('dev'));
app.use(app.router);
});
app.configure('development', function () {
app.use(express.errorHandler());
app.locals.pretty = true;
});
function urlRouter(req, res, controller, action, id, ispost) {
res.locals.session = req.session;
var controller = controller || 'home';
var action = action || 'index';
var ctr = controllers[controller]
try {
if (!ctr) {
ctr = controllers[controller] = require('./routes/' + controller);
}
if (ctr[action].needLogin && !req.session.user) {
res.redirect('/home/login');
} else if (ispost) {
ctr[action].post(req, res, id);
} else {
ctr[action].get(req, res, id);
}
} catch (err) {
console.error(err);
res.status(404);
res.render('layout', {error: err});
}
}
app.get('/', function (req, res) {
urlRouter(req, res, 'home', 'index');
});
app.get('/:controller/:action?/:id?', function (req, res) {
urlRouter(req, res, req.params.controller, req.params.action, req.params.id, false);
});
app.post('/:controller/:action?/:id?', function (req, res) {
urlRouter(req, res, req.params.controller, req.params.action, req.params.id, true);
});
exports.listen = listen = function(port){
app.listen(port);
console.log('http://127.0.0.1:' + app.get('port'))
}
if (!module.parent) {
listen(3000);
}