webpack-dev-server to return auto-generated for specific URL?

社会主义新天地 提交于 2019-12-12 05:13:52

问题


I have a single page app which connects to a back-end REST API on a different server.

On index.html app load, first thing it does is GET /env.json which contains the URL for the REST API server. Obviously, it is different in prod than in test than in dev.

Is there any way I can add middleware to the webpack-dev-server so that when it sees GET /env.json it serves up automatically generated json (based on env var or other)?

If there is an easier way, I am open to it. My assumption was that dev and test would start a backend server and then configure response to GET /env.json, while prod would have a different file added or dynamically generated.


回答1:


According to webpack-dev-server source code there is setup option which takes app (instance of express) variable as function argument. So, you can manipulate routes by specifying this option in your config:

devServer: {
    setup: function(app) {
        app.get('env.json', function(req, res) {
            // ...
        });
    }
}


来源:https://stackoverflow.com/questions/36215469/webpack-dev-server-to-return-auto-generated-for-specific-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!