gpt4 book ai didi

javascript - webpack dev server 代理 post 请求到本地 .json 文件

转载 作者:行者123 更新时间:2023-11-30 15:25:18 24 4
gpt4 key购买 nike

我手头有一个新项目,所以我没有任何服务器端支持,它们还没有准备好。

我想用本地 json 文件模拟我的 http 请求。

我有两个正在进行的请求:

  1. /entity/get方法是'GET'

  2. /entity/create 方法是'POST'

我有两个本地json文件

  1. 模拟/实体/get.json

  2. mock/entity/create.json

在我的配置文件中:

配置/index.js:

代理表:{

'/entity':{

target: 'http://localhost:8080/mock/',

pathRewrite(path) {

return url.parse(path).pathname + '.json';

}

}

构建/开发服务器.js

app.use('/mock', express.static('mock'));

结果:

  1. /entity/get,我得到我想要的。

  2. /entity/create, 404

我的问题:

  1. 为什么我无法让 express 服务器响应 create.json?

  2. 在 webpack1.0 中我有同样的问题,但我通过将所有 post 请求转换为 get 请求来处理这个问题,副作用是,我在 post-to-get 转换期间丢失了所有请求参数。我可以接受它,但在 webpack 2.0 中我什至不知道如何配置。

最佳答案

已解决问题 2:

配置/index.js:

proxyTable: {

'/entity':{

target: 'http://localhost:8080/mock/',

pathRewrite(path, option) {

option.method = 'GET';

return url.parse(path).pathname + '.json';

}

}

关于javascript - webpack dev server 代理 post 请求到本地 .json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43084148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com