zhangjiaqing
11 小时以前 87ec892b103e1e6ae4f9a1f06cb0837e422fbcda
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
var createError = require('http-errors');
var express = require('express');
const cors = require('cors');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
 
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
 
const proxy = require('express-http-proxy');
const { createProxyMiddleware } = require('http-proxy-middleware');
 
var app = express();
//在传输内容或者上传文件时,系统默认大小为100kb,这时,我们需要修改系统限制
//感谢 HC141221 兄弟 贡献 http://bbs.homecommunity.cn/pages/bbs/topic.html?topicId=102022101723500172
// 这里会影响文件上传
// var bodyParser = require('body-parser');
// app.use(bodyParser.json({
//     limit: '50mb'
// }));
// app.use(express.urlencoded({ limit: '50mb', extended: true }));
 
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
 
app.use(logger('dev'));
// 使用cors中间件
app.use(cors());
// 反向代理(这里把需要进行反代的路径配置到这里即可)
let opts = {
    preserveHostHdr: true,
    // 添加limit选项,支持50MB大文件上传
    limit: "50mb",
    //转发之前触发该方法
    proxyReqPathResolver: function (req, res) {
        //这个代理会把匹配到的url(下面的 ‘/api’等)去掉,转发过去直接404,这里手动加回来,
        req.url = req.baseUrl + req.url;
        //console.log(1,req,res)
        return require('url').parse(req.url).path;
    },
    // 修改代理请求的选项,包括请求头
    proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
        // 确保USER-ID请求头被传递
        if (!proxyReqOpts.headers['USER-ID']) {
            proxyReqOpts.headers['USER-ID'] = '-1';
        }
        // 确保VERSION请求头被传递
        if (!proxyReqOpts.headers['VERSION']) {
            proxyReqOpts.headers['VERSION'] = 'v1.8';
        }
        // 确保APP-ID请求头被传递
        if (!proxyReqOpts.headers['APP-ID']) {
            proxyReqOpts.headers['APP-ID'] = '8000418004';
        }
        // 确保TRANSACTION-ID请求头被传递
        if (!proxyReqOpts.headers['TRANSACTION-ID']) {
            proxyReqOpts.headers['TRANSACTION-ID'] = require('uuid').v4();
        }
        // 确保REQ-TIME请求头被传递
        if (!proxyReqOpts.headers['REQ-TIME']) {
            // 生成YYYYMMDDHHMISS格式的时间戳
            let now = new Date();
            let year = now.getFullYear();
            let month = String(now.getMonth() + 1).padStart(2, '0');
            let day = String(now.getDate()).padStart(2, '0');
            let hours = String(now.getHours()).padStart(2, '0');
            let minutes = String(now.getMinutes()).padStart(2, '0');
            let seconds = String(now.getSeconds()).padStart(2, '0');
            proxyReqOpts.headers['REQ-TIME'] = `${year}${month}${day}${hours}${minutes}${seconds}`;
        }
        return proxyReqOpts;
    }
}
 
// todo 测试环境 测试使用,生产环境请用nginx带来
// app.use('/callComponent', proxy('http://192.168.100.108:8088', opts));
// app.use('/app', proxy('http://192.168.100.108:8088', opts));
// app.use('/ws', createProxyMiddleware({
//     target: 'http://192.168.100.108:8008',
//     changeOrigin: true,
//     ws: true
// }));
 
// app.use('/ws', proxy('http://192.168.100.109:8008', opts));
// app.use('/callComponent', proxy('http://192.168.100.109:8008', opts));
// app.use('/app', proxy('http://192.168.100.109:8008', opts));
 
// todo 本机 开发用,生产环境请用nginx带来
//  app.use('/ws', proxy('http://192.168.31.222:8008', opts));
// app.use('/callComponent', proxy('http://192.168.31.222:8008', opts));
//  app.use('/app', proxy('http://192.168.31.222:8008', opts));
 
app.use('/ws', createProxyMiddleware({
    target: 'http://192.168.31.179:8008',
    changeOrigin: true,
    ws: true
}));
app.use('/callComponent', proxy('http://192.168.31.179:8008', opts));
app.use('/app', proxy('http://192.168.31.179:8008', opts));
// .split(' ')[0]
 
// todo 本机 开发用,生产环境请用nginx带来
// app.use('/ws', proxy('http://47.92.223.85:8008', opts));
//  app.use('/callComponent', proxy('http://47.92.223.85:8008', opts));
//  app.use('/app', proxy('http://47.92.223.85:8008', opts));
 
//app.listen(3000);
// 增加默认中间件的大小限制,支持大文件上传
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ limit: '50mb', extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
 
app.use('/', indexRouter);
app.use('/users', usersRouter);
 
 
// catch 404 and forward to error handler
app.use(function (req, res, next) {
    next(createError(404));
});
 
// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
 
    // render the error page
    res.status(err.status || 500);
    res.render('error');
});
 
module.exports = app;