gpt4 book ai didi

parceljs - 有没有办法像在 Webpack 中一样在 Parcel 中代理请求?

转载 作者:行者123 更新时间:2023-12-05 00:46:43 25 4
gpt4 key购买 nike

在 Webpack 中,可以通过 proxy 代理后端请求。在配置文件中设置。这允许我使用带有 HMR 的 webpack-dev-server 开发我的应用程序的前端部分,而 webpack-dev-server 和我的应用程序服务器在我的本地主机上的不同端口上运行。 Parcel 中还有一个开发服务器,默认运行命令 parcel index.html在端口 1234 上。有没有办法同时对我的应用服务器运行 Parcel 开发服务器和代理请求?

我找到了一个建议使用 Express 中间件的解决方案。但这并不能完全干净地解决问题。如果我的后端运行 Django 怎么办?那我应该如何使用 Parcel 开发服务器呢?

最佳答案

目前不直接支持此功能,请参阅 open pull-request https://github.com/parcel-bundler/parcel/pull/2477

然而,https://github.com/parcel-bundler/parcel/issues/55并列出了涉及简单包装器的各种解决方案,例如:

http-proxy-middleware >= 1.0.0(2020 年 2 月发布):

const Bundler = require('parcel-bundler');
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');


const app = express();

app.use(createProxyMiddleware('/api', {
target: 'http://localhost:3000'
}));

const bundler = new Bundler('src/index.html');
app.use(bundler.middleware());

app.listen(Number(process.env.PORT || 1234));


老年人 http-proxy-middleware (版本 0.x):

const Bundler = require('parcel-bundler');
const express = require('express');
const proxy = require('http-proxy-middleware');

const app = express();

app.use('/api', proxy({
target: 'http://localhost:3000/api'
}));

const bundler = new Bundler('src/index.html');
app.use(bundler.middleware());

app.listen(Number(process.env.PORT || 1234));

关于parceljs - 有没有办法像在 Webpack 中一样在 Parcel 中代理请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53902896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com