Node.js yog framework has been in the field for more than a year.

In the last year, I used the yog2 framework in some practical aspects of the service, and some thoughts should be sent. Now, the architecture of the web is based on the node.js yog2 framework.

describe

The YOG2 framework provides automatic routing and multi-level routing system based on express routing. The goal is to make the UI layer more efficient, more formal, and more flexible.

YOG2’s route is divided into two classes. One is the root route, whose role is very similar to the traditional Express route, which is the consistent entry of all requests, so we have the maximum degree of freedom in the root route and can do any forwarding. The other type is app routing, which can only receive requests from root routes to various apps. How can the requests be distributed in the app and can be completely controlled by the app?

advantage

  • Real-time compilation of fis3 is extremely efficient

  • Routing automatic matching

  • Project split, the concept of modularity is very strong.

  • Node. Js express extension

  • Yog2 provides the logging module

  • Yog2 provides ral for centralized processing of service requests.

disadvantages

  • The layout of the project organization is chaotic.

  • The default template engine swig is hard to use.

case

  • Quickly generate a project, node server project shelf.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Initializing a yog2 project, which mainly includes the basic project directory structure and the basic framework code.
yog2 init project
//Initializes a yog2 app, which mainly contains the basic app directory structure and some sample files.
yog2 init app
//Project start
cd frame
yog2 run
//Real-time compilation
cd frame
cd frame/home
yog2 release --dest debug -wp
//The deployment of Compile to public files.
yog2 release --dest ../../publish -po

PM2 deployment project

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 cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var yog = require('yog2-kernel');
if (cluster.isMaster) {
require('os').cpus().forEach(function () {
cluster.fork();
});
cluster.on('exit', function (worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
}
else {
var app = yog.bootstrap({
rootPath: __dirname
}, function () {
console.log('plugins load completed');
});
app.set('port', process.env.PORT || 8085);
app.disable('x-powered-by');
var server = yog.server = app.listen(app.get('port'), function () {
console.log('Yog server listening on port ' + server.address().port);
});
server.on('connection', function (socket) {
// disable nagle
socket.setNoDelay(true);
});
// 仅在 Node.js 6.x开启这个功能 https://github.com/nodejs/node/issues/7126
if (parseInt(process.versions.node.split('.')[0], 10) >= 6) {
server.on('clientError', function (err, socket) {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
}
}

Project directory

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
//A complete YOG2 directory structure should be similar.
├─home
│ ├─client
│ │ ├─page
│ │ ├─static
│ │ └─widget
│ └─server
│ ├─action
│ ├─lib
│ └─model
├─user
│ ├─client
│ │ ├─page
│ │ ├─static
│ │ └─widget
│ └─server
│ ├─action
│ ├─lib
│ └─model
└─yog
├─app
├─bin
├─conf
│ ├─plugins
│ └─ral
├─plugins
├─static
└─views
//Project directory
├─yog
├─app # server代码目录
├─conf # 配置目录
│ ├─plugins # 插件配置
│ └─ral # 后端服务配置
├─plugins # 插件目录
├─static # 静态资源目录
├─views # 后端模板目录
└─app.js # project 启动入口
//App directory
├─client # 前端代码
│ ├─page # 前端页面
│ ├─static # 前端非模块化静态资源
│ │ ├─css
│ │ └─js
│ └─widget # 前端组件
├─fis-conf.js # FIS编译配置
└─server # 后端代码
├─action # Action是指MVC中的路由动作,处理页面请求
├─lib # 可以存放一些通用库
├─model # 可以存放一些数据层代码,如后端API请求等
└─router.js # AppRouter路由,用于处理自动路由无法满足的需求