我如何让webpack-dev-server接受POST请求 [英] How do I get webpack-dev-server to accept POST requests

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

问题描述

在我的项目中,我致电:

In my project I call:

$ webpack-dev-server --history-api-fallback

它会启动一个在localhost:8080上可用的快递服务器(我假设).

And it starts an express server (I'm assuming) available on localhost:8080.

除了我想通过POST将表单提交到加载我的应用程序的iframe中之外,它的工作原理非常好; localhost:8080正在开发中,而其他正在生产中.

It works great except that I want to submit a form via POST into an iframe loading my app; localhost:8080 in development and something else in production.

我不希望在开发中对POST数据做任何事情,但是在生产中,它必须是POST.

I don't expect to do anything with that POST data in development, but in production, it needs to be POST.

但是,当我尝试使用POST时,它cannot find POST /

When I try to use POST however, it cannot find POST /

是否有允许我使用webpack-dev-server的配置选项或其他解决方案? (我真的不想为此编写自己的服务器).

Is there a configuration option or some other solution that will allow me to use the webpack-dev-server? (I really don't want to have to write my own server for this).

推荐答案

@cowCrazy有一半的故事,但是没有显示如何在POST的情况下对GET做出相同的响应寻找.

@cowCrazy had half of the story, but didn't show how to respond with the same response for GET in the case of POST, which is what I was looking for.

在Webpack配置中,您可以执行以下操作:

In the Webpack config, you do this:

module.exports = {
    ...,
    devServer: {
        setup(app) {
            app.post('*', (req, res) => {
                res.redirect(req.originalUrl);
            });
        },
    }
}

这将仅使用POST URL并使用相同的URL重定向,使其成为GET请求.

This will just take the POST URL and redirect with the same URL, making it a GET request.

它不会出现服务器错误,而是会使用GET重定向.

Instead of having a server error, it will redirect with GET.

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

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