Node.js expres builds some ideas for large projects

My thoughts, actually very simple, just want to build a framework based on node server rendering, supports real-time compilation, project structure similar to a traditional MVC, the m layer handles the service request, v is responsible for rendering, c control routing, yog2 framework USES more than a year, feeling lost the advantage of the node express

model The logic of the model looks like this.

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
var axios = require('axios');
var querystring = require('querystring');
var instance = axios.create({
baseURL: "https://some-domain.com/api/",
timeout: 1000,
headers: { 'X-Custom-Header': 'foobar' }
});
/* http get */
exports.get = function(params, callback) {
axios.get(params.url)
.then(function(response) {
return callback(response)
})
.catch(function(err) {
console.log(err);
});
};
/* http post */
exports.post = function(params, callback) {
axios.post(params.url, params.body)
.then(function(response) {
return callback(response)
})
.catch(function(err) {
console.log(err);
});
};
/* http delete */
exports.delete = function(params, callback) {
axios.delete(params.url)
.then(function(response) {
return callback(response)
})
.catch(function(err) {
console.log(err);
});
};

The routing layer looks like this.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 集成所有的路由 */
var User = require('../app/controllers/user')
var Index = require('../app/controllers/index')
var Comment = require('../app/controllers/comment')
module.exports = function(app) {
// pre handle user
/* app.use(function(req, res, next) {
var _user = req.session.user
app.locals.user = _user
next()
}) */
// 该路由使用的中间件
app.use(function timeLog(req, res, next) {
console.log('Time: ', Date.now());
next();
});
// Index
app.get('/', Index.index);
// Comment
/* 路由级别的中间件 判断没有登录不能产看 */
app.post('/user/comment', User.signinRequired, Comment.save)
}
/* index pages */
exports.index = function(req, res) {
res.render('index.html', { title: 'Birds home page' });
}
var mongoose = require('mongoose')
var User = mongoose.model('User')
// signup
exports.showSignup = function(req, res) {
res.render('signup', {
title: '注册页面'
})
}
exports.showSignin = function(req, res) {
res.render('signin', {
title: '登录页面'
})
}
exports.signup = function(req, res) {
var _user = req.body.user
User.findOne({ name: _user.name }, function(err, user) {
if (err) {
console.log(err)
}
if (user) {
return res.redirect('/signin')
} else {
user = new User(_user)
user.save(function(err, user) {
if (err) {
console.log(err)
}
res.redirect('/')
})
}
})
}
// signin
exports.signin = function(req, res) {
var _user = req.body.user
var name = _user.name
var password = _user.password
User.findOne({ name: name }, function(err, user) {
if (err) {
console.log(err)
}
if (!user) {
return res.redirect('/signup')
}
user.comparePassword(password, function(err, isMatch) {
if (err) {
console.log(err)
}
if (isMatch) {
req.session.user = user
return res.redirect('/')
} else {
return res.redirect('/signin')
}
})
})
}
// logout
exports.logout = function(req, res) {
delete req.session.user
//delete app.locals.user
res.redirect('/')
}
// userlist page
exports.list = function(req, res) {
User.fetch(function(err, users) {
if (err) {
console.log(err)
}
res.render('userlist', {
title: 'imooc 用户列表页',
users: users
})
})
}
// midware for user
exports.signinRequired = function(req, res, next) {
var user = req.session.user
if (!user) {
return res.redirect('/signin')
}
next()
}
exports.adminRequired = function(req, res, next) {
var user = req.session.user
if (user.role <= 10) {
return res.redirect('/signin')
}
next()
}
/* Comment pages */
exports.save = function(req, res) {
res.render('index.html', { title: 'Birds Comment page' });
}