Commit eb520b25 authored by 张烨's avatar 张烨

refactor: update coding about server proxy

parent 3157c448
......@@ -39,7 +39,7 @@
"lint": "npm run lint:js && npm run lint:css",
"lint:css": "stylelint src/**/*.less",
"lint:eslint": "eslint --ignore-path .gitignore --ignore-pattern internals/scripts",
"lint:eslint:fix": "eslint --ignore-path .gitignore --ignore-pattern internals/scripts --fix",
"lint:fix": "eslint --ignore-path .gitignore --ignore-pattern internals/scripts --fix -- .",
"lint:js": "npm run lint:eslint -- . ",
"lint:staged": "lint-staged",
"pretest": "npm run test:clean && npm run lint",
......@@ -67,7 +67,7 @@
},
"lint-staged": {
"*.js": [
"npm run lint:eslint:fix",
"npm run lint:fix",
"git add --force"
],
"*.json": [
......@@ -217,6 +217,6 @@
"webpack-pwa-manifest": "4.0.0",
"whatwg-fetch": "3.0.0",
"yorkie": "^2.0.0",
"express-http-proxy": "^1.6.2"
"http-proxy-middleware": "^1.0.6"
}
}
......@@ -14,6 +14,10 @@ const logger = {
console.error(chalk.red(err));
},
info: str => {
console.log(chalk.green(str));
},
// Called when express.js app starts on given port w/o errors
appStarted: (port, host, tunnelStarted) => {
console.log(`Server started ! ${chalk.green('✓')}`);
......
......@@ -2,12 +2,8 @@ const path = require('path');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const {
createProxyMiddleware,
Filter,
Options,
RequestHandler,
} = require('http-proxy-middleware');
const { createProxyMiddleware } = require('http-proxy-middleware');
const logger = require('../logger');
function createWebpackMiddleware(compiler, publicPath) {
return webpackDevMiddleware(compiler, {
......@@ -32,6 +28,7 @@ module.exports = function addDevMiddlewares(app, webpackConfig) {
// artifacts, we use it instead
const fs = middleware.fileSystem;
let proxyHost = process.env.PROXY;
const proxyPrefix = process.env.PROXY_PREFIX || '/Cityinterface';
app.get('/setproxy', (req, res) => {
if (req.query && req.query.host) {
proxyHost = req.query.host;
......@@ -47,13 +44,26 @@ module.exports = function addDevMiddlewares(app, webpackConfig) {
proxyHost.toLowerCase() !== 'off' ||
proxyHost !== '0')
) {
logger.info(`using proxy at: ${proxyHost}`);
logger.info(`proxy prefix: ${proxyPrefix}`);
app.use(
process.env.PROXY_PREFIX || '/Cityinterface',
createProxyMiddleware({ target: proxyHost, changeOrigin: true }),
proxyPrefix,
createProxyMiddleware({
target: proxyHost,
changeOrigin: true,
// eslint-disable-next-line no-unused-vars
onProxyReq: (_prexyReq, _req, _res) => {
logger.info('onProxyReq');
},
// eslint-disable-next-line no-unused-vars
onProxyRes(_proxyRes, _req, _res) {
logger.info('onProxyRes');
},
}),
);
}
app.get('*', (req, res) => {
app.get('*', (_req, res) => {
fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
if (err) {
res.sendStatus(404);
......
......@@ -4,6 +4,7 @@ import { PageContainer } from '@ant-design/pro-layout';
import { connect } from 'react-redux';
import styles from './Welcome.less';
// eslint-disable-next-line no-unused-vars
const CodePreview = ({ children }) => (
<pre className={styles.pre}>
<code>
......
/* eslint-disable*/
import React, { useState } from 'react';
import { Form, Input, Button, Space, Select } from 'antd';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
......@@ -28,17 +27,13 @@ const RequestTest = () => {
console.error(err);
});
} else {
get('/setproxy', { host: url.match(/^(https?:\/\/[^\/]+)/)[1] }).then(
res => {
const proxyUrl = url.replace(/^https?:\/\/([^\/]+)/, $1 => {
return `${window.location.origin}`;
});
methodMap[method](proxyUrl, params)
.catch(err => {
console.error(err);
});
},
);
const proxyUrl = url.replace(/^https?:\/\/([^\/]+)/, $1 => {
return `${window.location.origin}`;
});
methodMap[method](proxyUrl, params)
.catch(err => {
console.error(err);
});
}
};
......
......@@ -36,13 +36,13 @@ const Login = props => {
)
.then(result => {
console.log(result.data);
if (result.data.success == true && result.data.pass == true) {
if (result.data.success === true && result.data.pass === true) {
const { userMode } = result.data;
localStorage.setItem('userMode', userMode);
if (userMode == 'admin' || userMode == 'super') {
if (userMode === 'admin' || userMode === 'super') {
window.location.href = '/dbm/dbInit/';
}
if (userMode == 'common') {
if (userMode === 'common') {
window.location.href = '/ou/orgList/';
}
} else {
......
import { request } from '../utils/request';
const axios = request.defaults;
const get = async (url, params, options = {}) =>
request({
url,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment