fix(fes-cli): 修复mock对post请求的支持

post请求时对body内容没进行json解析,导致无法mock;同时修复proxy对post请求的支持
This commit is contained in:
万纯 2020-11-27 11:06:58 +08:00
parent 97f35967af
commit e4815cb81d
2 changed files with 42 additions and 29 deletions

View File

@ -143,7 +143,7 @@ module.exports = function webpackConfig(configs, webpack, mode) {
{
loader: require.resolve('cache-loader'),
options: {
cacheDirectory: path.resolve(configs.folders.PROJECT_DIR, 'node_modules/.cache/vue-loader')
cacheDirectory: path.resolve(configs.folders.PROJECT_DIR, '.cache/vue-loader')
}
},
{
@ -152,7 +152,7 @@ module.exports = function webpackConfig(configs, webpack, mode) {
compilerOptions: {
preserveWhitespace: false
},
cacheDirectory: path.resolve(configs.folders.PROJECT_DIR, 'node_modules/.cache/vue-loader')
cacheDirectory: path.resolve(configs.folders.PROJECT_DIR, '.cache/vue-loader')
}
}
]
@ -303,7 +303,7 @@ module.exports = function webpackConfig(configs, webpack, mode) {
{
loader: require.resolve('cache-loader'),
options: {
cacheDirectory: path.resolve(configs.folders.PROJECT_DIR, 'node_modules/.cache/babel-loader')
cacheDirectory: path.resolve(configs.folders.PROJECT_DIR, '.cache/babel-loader')
}
},
{
@ -325,7 +325,9 @@ module.exports = function webpackConfig(configs, webpack, mode) {
devtool: isDev && 'cheap-module-eval-source-map',
plugins: [
new HardSourceWebpackPlugin(),
new HardSourceWebpackPlugin({
cacheDirectory: path.resolve(configs.folders.PROJECT_DIR, '.cache/hard-source')
}),
/* config.plugin('progress') */
new webpack.ProgressPlugin(),

View File

@ -15,28 +15,6 @@ const util = require('./util');
const router = express.Router();
const proxy = httpProxy.createProxyServer();
proxy.on('open', (proxySocket) => {
proxySocket.on('data', (chunk) => {
log.message(chunk.toString());
});
});
proxy.on('proxyRes', (proxyRes) => {
log.message(
'RAW Response from the target',
JSON.stringify(proxyRes.headers, true, 2)
);
const cookie = proxyRes.headers['set-cookie'];
if (cookie && cookie.length > 0) {
for (let i = 0; i < cookie.length; i++) {
cookie[i] = cookie[i].replace('Secure', '');
}
}
});
proxy.on('error', (e) => {
log.error(e);
});
// 根据参数个数获取配置
function getOption(arg) {
@ -135,6 +113,41 @@ const createMock = function () {
};
cgiMock.proxy = function (host) {
const proxy = httpProxy.createProxyServer();
proxy.on('open', (proxySocket) => {
proxySocket.on('data', (chunk) => {
log.message(chunk.toString());
});
});
proxy.on('proxyReq', (proxyReq, req) => {
proxyReq.setHeader('Host', url.parse(host).host);
if (req.body) {
const bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader('Content-Type', 'application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// stream the content
proxyReq.write(bodyData);
}
});
proxy.on('proxyRes', (proxyRes) => {
log.message(
'RAW Response from the target',
JSON.stringify(proxyRes.headers, true, 2)
);
const cookie = proxyRes.headers['set-cookie'];
if (cookie && cookie.length > 0) {
for (let i = 0; i < cookie.length; i++) {
cookie[i] = cookie[i].replace('Secure', '');
}
}
});
proxy.on('error', (e) => {
log.error(e);
});
proxy.on('proxyReq', (proxyReq) => {
proxyReq.setHeader('Host', url.parse(host).host);
});
process.nextTick(() => {
router.use((req, res) => {
proxy.web(req, res, {
@ -143,9 +156,6 @@ const createMock = function () {
});
});
});
proxy.on('proxyReq', (proxyReq) => {
proxyReq.setHeader('Host', url.parse(host).host);
});
};
return cgiMock;
@ -192,6 +202,7 @@ const loadCustomRoute = function (app) {
module.exports = function (app) {
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: false