Javascript 如何使用webpack proxy devserver pathRewrite?
我目前正在努力重写api服务器的代理路径。 在我的设置中,我所做的是针对api请求,我将其委托给代理服务器,并且仅针对js/html/cssJavascript 如何使用webpack proxy devserver pathRewrite?,javascript,angular,webpack,webpack-dev-server,webpack-4,Javascript,Angular,Webpack,Webpack Dev Server,Webpack 4,我目前正在努力重写api服务器的代理路径。 在我的设置中,我所做的是针对api请求,我将其委托给代理服务器,并且仅针对js/html/csswebpack dev server 以下是我正在使用的内容: devServer: { inline: true, port: 8080, historyApiFallback: true, publicPath: 'http://localhost:8080/dist/', disableHostCheck: tr
webpack dev server
以下是我正在使用的内容:
devServer: {
inline: true,
port: 8080,
historyApiFallback: true,
publicPath: 'http://localhost:8080/dist/',
disableHostCheck: true,
proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: {'???' : ''} //Need to append http://localhost:3000/MySite1/api
}
}
那么,在将/MySite1代理到localhost:3000之前,如何将其追加到api请求中
例如。
如果请求是:
,则应重新写入
而且,
如果请求是:
,
它应该重新写入到Create proxy.config.json
{
"/api/*": {
"target": "http://localhost:3000/MySite1/api",
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true,
"secure": false,
"logLevel": "debug"
}
}
^/api部分将替换为目标
然后用
ng serve --proxy-config proxy.config.json
请尝试以下操作:
devServer: {
inline: true,
port: 8080,
historyApiFallback: true,
publicPath: 'http://localhost:8080/dist/',
disableHostCheck: true,
proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: function(path, req) {
var replacedPath = path;
if (path.indexOf("MySite1") === -1) {
replacedPath = replacedPath.replace("/", "/MySite1/api/");
}
return replacedPath;
},
}
}
我想在/api not ignore之前附加
/MySite1
,对不起,我仍在编辑我的文件以匹配您的文件,这应该有效否,我的意思是,如果请求是,它应该将其重新写入