webpack-dev-server 返回自动生成的特定 URL? [英] webpack-dev-server to return auto-generated for specific URL?
问题描述
我有一个单页应用程序,它连接到不同服务器上的后端 REST API.
I have a single page app which connects to a back-end REST API on a different server.
在 index.html
应用加载时,它做的第一件事是 GET/env.json
,其中包含 REST API 服务器的 URL.很明显,它在 prod 和 test 和 dev 中是不同的.
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.
有什么方法可以将中间件添加到 webpack-dev-server,以便当它看到 GET/env.json
时,它会提供自动生成的 json(基于 env var 或其他)?
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)?
如果有更简单的方法,我愿意接受.我的假设是 dev 和 test 将启动后端服务器,然后配置对 GET/env.json
的响应,而 prod 将添加或动态生成不同的文件.
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.
推荐答案
根据 webpack-dev-server 源代码 有一个 setup
选项,它将 app
(express 实例)变量作为函数参数.因此,您可以通过在配置中指定此选项来操作路由:
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) {
// ...
});
}
}
这篇关于webpack-dev-server 返回自动生成的特定 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!