@gingkoo/base-server 0.0.1-alpha.1 → 0.0.1-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/app.js CHANGED
@@ -15,7 +15,8 @@ const router = require('./backend/router');
15
15
  class App {
16
16
  /**
17
17
  * @param {string} optinos.timeout 超时时间 默认 30s
18
- * @param {(app: any) => void} optinos.routes
18
+ * @param {(app: any, express: any) => void} optinos.beforeRoutes 在前面注册路由
19
+ * @param {(app: any, express: any) => void} optinos.afterRoutes 在后面注册路由
19
20
  */
20
21
  constructor(options = {}) {
21
22
  this.options = options;
@@ -65,10 +66,13 @@ class App {
65
66
  }
66
67
 
67
68
  routes() {
68
- if (this.options.routes) {
69
- this.options.routes(this.app);
69
+ if (this.options.beforeRoutes) {
70
+ this.options.beforeRoutes(this.app, express);
70
71
  }
71
72
  this.app.use('/', router);
73
+ if (this.options.afterRoutes) {
74
+ this.options.afterRoutes(this.app, express);
75
+ }
72
76
  }
73
77
 
74
78
  listenSocket() {
package/backend/router.js CHANGED
@@ -27,6 +27,33 @@ router.use('/fss', fss);
27
27
  router.use('/wechat', wechat);
28
28
  router.use('/space', space);
29
29
 
30
+ // 前端
31
+ router.use('/static', express.static(path.join(__dirname, '../static')));
32
+
33
+ if (config.isProd) {
34
+ router.use('/', express.static(path.join(__dirname, '../dist')));
35
+ } else {
36
+ router.use(
37
+ '/',
38
+ createProxyMiddleware({
39
+ target: 'http://127.0.0.1:2000',
40
+ changeOrigin: false,
41
+ ws: true,
42
+ pathRewrite: {
43
+ '^/': '',
44
+ },
45
+ onProxyReq: function (proxyReq, req, res) {
46
+ if (req.body) {
47
+ let bodyData = JSON.stringify(req.body);
48
+ proxyReq.setHeader('Content-Type', 'application/json');
49
+ proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
50
+ proxyReq.write(bodyData);
51
+ }
52
+ },
53
+ }),
54
+ );
55
+ }
56
+
30
57
  // 反向代理
31
58
  if (Array.isArray(config?.proxy?.targets)) {
32
59
  config.proxy.targets.forEach((element) => {
@@ -46,8 +73,8 @@ if (Array.isArray(config?.proxy?.targets)) {
46
73
  let tokenData = jwt.verifyToken(req.cookies['X-Token']);
47
74
  if (tokenData) {
48
75
  proxyReq.setHeader('X-Token', req.cookies['X-Token']);
49
- proxyReq.setHeader('X-UserId', tokenData['userid']);
50
- proxyReq.setHeader('X-OrgId', tokenData['orgid']);
76
+ tokenData?.data?.userid && proxyReq.setHeader('X-UserId', tokenData.data.userid);
77
+ // proxyReq.setHeader('X-OrgId', tokenData['orgid']);
51
78
  } else {
52
79
  proxyReq.removeHeader('X-UserId');
53
80
  }
@@ -66,31 +93,4 @@ if (Array.isArray(config?.proxy?.targets)) {
66
93
  });
67
94
  }
68
95
 
69
- // 前端
70
- router.use('/static', express.static(path.join(__dirname, '../static')));
71
-
72
- if (config.isProd) {
73
- router.use('/', express.static('dist'));
74
- } else {
75
- router.use(
76
- '/',
77
- createProxyMiddleware({
78
- target: 'http://127.0.0.1:2000',
79
- changeOrigin: false,
80
- ws: true,
81
- pathRewrite: {
82
- '^/': '',
83
- },
84
- onProxyReq: function (proxyReq, req, res) {
85
- if (req.body) {
86
- let bodyData = JSON.stringify(req.body);
87
- proxyReq.setHeader('Content-Type', 'application/json');
88
- proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
89
- proxyReq.write(bodyData);
90
- }
91
- },
92
- }),
93
- );
94
- }
95
-
96
96
  module.exports = router;