Webpack不接受POST请求 [英] Webpack not accepting POST requests

查看:179
本文介绍了Webpack不接受POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用与API通信的Angular 2 Web应用程序.在该应用程序中,用户可以选择付款方式,API会将网址返回到付款服务.

I'm currently working on an Angular 2 web application which communicates with an API. In the application the user is able to choose a payment option and the API will return the url to the payment service.

问题是付款服务使用POST转到WebPack不接受的确认页面(由于某种原因,它仅允许GET请求),并且出现以下错误:

The problem is that the payment service uses POST to go to the confirmation page which WebPack does not accept (for some reason it only allows GET requests) and we get the following error:

Cannot POST /selection/payment-method

有人知道我们如何配置WebPack还允许POST请求吗?我已经联系了支付提供商,但是无法执行GET请求而不是POST.

Does anybody know how we could configure that WebPack allows POST requests also? I've contacted the payment provider but it is not possible to do a GET request instead of POST.

谢谢

推荐答案

感谢@robertklep,他向我发送了指向代理文档的链接,我们找到了一种处理它的方法.我们需要做的是代替处理POST请求,而是将其转换为GET.阅读更多文档后,我们发现了webpack-dev-server配置的setup:属性.

Thanks to @robertklep who send me the link to the proxy documentation we found a way to deal with it. What we needed to do was instead of handling the POST request we needed to transform it someway into a GET. After reading some more of the documentation we came across the setup: property for your webpack-dev-server configuration.

使用setup:属性,您可以获得expressjs对象,并且可以在URL到达表示Cannot POST /url/to/page的路由之前捕获URL.

With the setup: property you get the expressjs object and you are able to catch urls before it hits the route that says Cannot POST /url/to/page.

我们最终得到了这个

devServer: {
    setup: function(app) {
        app.post('/selection/payment-method', function(req, res) {
          res.redirect('/selection/payment-method');
        });
    },
}

这样,我们收到了GET请求而不是POST,我们的应用程序发出了API请求以检查付款是否成功.

This way we got a GET request instead of POST and our application does an API request to check if the payment succeeded or not.

这仅用于开发中!

这篇关于Webpack不接受POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆