ANe*_*own 2 javascript webpack webpack-dev-server angular webpack-4
我目前正在努力重写 api 服务器的代理路径。在我的设置中,我所做的是 api 请求,我将它委托给代理服务器,并且仅使用 js/html/css 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
}
}
Run Code Online (Sandbox Code Playgroud)
那么,如何在代理到 localhost:3000 之前将 /MySite1 附加到 api 请求?
例如,如果请求是: http://localhost:8080/api,它应该重新写入http://localhost:3000/MySite1/api
另外,如果请求是: http://localhost:8080,它应该重新写入http://localhost:3000/MySite1
创建 proxy.config.json
{
"/api/*": {
"target": "http://localhost:3000/MySite1/api",
"pathRewrite": {
"^/api": ""
},
"changeOrigin": true,
"secure": false,
"logLevel": "debug"
}
}
Run Code Online (Sandbox Code Playgroud)
^/api 部分将替换为 target
然后启动应用程序
ng serve --proxy-config proxy.config.json
Run Code Online (Sandbox Code Playgroud)
尝试以下操作:
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;
},
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7242 次 |
| 最近记录: |