Webpack devServer 代理不适用于 Sapper

Webpack devServer proxy not working with Sapper

我在 http://localhost:3000 上有一个 node 开发服务器 运行 Sapper,我想要所有 /api/ 请求代理写在 python http://localhost:8000/api/

上的另一个本地开发服务器

这对纯 Svelte 非常有效:

// webpack.config.js

module.exports.devServer = {
    historyApiFallback: true,
    proxy: {
        '/api/': {
            target: 'http://localhost:8000',
            secure: false,
            changeOrigin: true
        }
    },
};

但对 Sapper 完全没有任何作用 - 只是得到默认的 Sapper 的 404 错误

我猜它与Sapper的路由机制有某种关系,但找不到如何处理它

Sapper 使用 Polka server. Proxy can be configured using http-proxy-middleware

src/server.js

const { createProxyMiddleware } = require('http-proxy-middleware');

polka()
    .use('/api', createProxyMiddleware({ target: 'http://localhost:8000' }))
    // other .use, .listen rules