| | |
| | | /** |
| | | * 应用主文件 |
| | | * 功能:配置Express应用,设置中间件,配置代理和路由 |
| | | * |
| | | * 修改记录: |
| | | * 1. 添加了详细的注释,解释Express配置、中间件设置和代理配置 |
| | | * 2. 解释了文件大小限制和错误处理 |
| | | * 3. 确保了API代理配置正确 |
| | | * 4. 优化了中间件顺序,确保CORS在最前面 |
| | | * 5. 添加了文件上传大小限制的详细说明 |
| | | */ |
| | | |
| | | // 导入必要的模块 |
| | | var createError = require('http-errors'); |
| | | var express = require('express'); |
| | | const cors = require('cors'); |
| | |
| | | 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'); |
| | | |
| | | // 创建Express应用实例 |
| | | var app = express(); |
| | | |
| | | // 配置文件上传大小限制 |
| | | // 在传输内容或者上传文件时,系统默认大小为100kb,这时,我们需要修改系统限制 |
| | | // 感谢 HC141221 兄弟 贡献 http://bbs.homecommunity.cn/pages/bbs/topic.html?topicId=102022101723500172 |
| | | //在传输内容或者上传文件时,系统默认大小为100kb,这时,我们需要修改系统限制 |
| | | //感谢 HC141221 兄弟 贡献 http://bbs.homecommunity.cn/pages/bbs/topic.html?topicId=102022101723500172 |
| | | // 这里会影响文件上传 |
| | | var bodyParser = require('body-parser'); |
| | | app.use(bodyParser.json({ |
| | | limit: '50mb' // JSON数据大小限制为50MB |
| | | })); |
| | | app.use(express.urlencoded({ limit: '50mb', extended: true })); // URL编码数据大小限制为50MB |
| | | // 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中间件,允许跨域请求 |
| | | // 使用cors中间件 |
| | | app.use(cors()); |
| | | |
| | | // 反向代理配置(这里把需要进行反代的路径配置到这里即可) |
| | | // 反向代理(这里把需要进行反代的路径配置到这里即可) |
| | | let opts = { |
| | | preserveHostHdr: true, // 保留原始请求头 |
| | | reqAsBuffer: true, // 将请求作为Buffer处理 |
| | | limit: "50mb", // 代理请求大小限制为50MB |
| | | // 转发之前触发该方法 |
| | | preserveHostHdr: true, |
| | | reqAsBuffer: true, |
| | | limit: "50mb", |
| | | //转发之前触发该方法 |
| | | proxyReqPathResolver: function (req, res) { |
| | | // 这个代理会把匹配到的url(下面的 '/api'等)去掉,转发过去直接404,这里手动加回来 |
| | | //这个代理会把匹配到的url(下面的 ‘/api’等)去掉,转发过去直接404,这里手动加回来, |
| | | req.url = req.baseUrl + req.url; |
| | | //console.log(1,req,res) |
| | | return require('url').parse(req.url).path; |
| | |
| | | |
| | | } |
| | | |
| | | // 测试环境代理配置(注释状态) |
| | | // 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('/app', proxy('http://192.168.100.108:8088', opts)); |
| | | // app.use('/ws', createProxyMiddleware({ |
| | | // target: 'http://192.168.100.108:8008', |
| | | // changeOrigin: true, |
| | | // 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带来 本地 |
| | | // 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('/callComponent', proxy('http://192.168.31.222:8008', opts)); |
| | | // app.use('/app', proxy('http://192.168.31.222: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.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)); |
| | | |
| | | // 监听端口(注释状态,由bin/www文件启动) |
| | | //app.listen(3000); |
| | | |
| | | // 使用内置中间件 |
| | | app.use(express.json()); |
| | | app.use(express.urlencoded({ extended: false })); |
| | | app.use(cookieParser()); |
| | | app.use(express.static(path.join(__dirname, 'public'))); // 静态文件服务 |
| | | app.use(express.static(path.join(__dirname, 'public'))); |
| | | |
| | | // 配置路由 |
| | | app.use('/', indexRouter); |
| | | app.use('/users', usersRouter); |
| | | |
| | | // 404错误处理 |
| | | |
| | | // 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.render('error'); |
| | | }); |
| | | |
| | | // 导出应用实例 |
| | | module.exports = app; |