Commit eb520b25 authored by 张烨's avatar 张烨

refactor: update coding about server proxy

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