由于@nuxt/proxy
还没有为 Nuxt 3 做好准备,我偷了代码并暂时使用了自己的模块。不幸的是,代理似乎只在开发模式下工作。一旦我构建并运行服务器,代理就会被禁用。分解到最低限度,该模块是:
import { addServerMiddleware, defineNuxtModule } from "@nuxt/kit-edge";
import { createProxyMiddleware } from "http-proxy-middleware";
export default defineNuxtModule({
setup(options, nuxt) {
addServerMiddleware({
handler: createProxyMiddleware(
"/uploads",
{ target: "http://localhost:1337/" }
),
});
},
});
我的相关部分nuxt.config.js
是:
export default defineNuxtConfig({
buildModules: ["~/modules/proxy"],
[...]
});
我也尝试将其移至modules
:
export default defineNuxtConfig({
modules: ["~/modules/proxy"],
[...]
});
在构建期间,模块明显运行,createProxyMiddleware
将代理记录到控制台:
> nuxi build
Nuxt CLI v3.0.0-27369360.33ebb01 14:00:06
ℹ [HPM] Proxy created: /uploads -> http://localhost:1337/ 14:00:08
ℹ Vite warmed up in 3517ms 14:00:13
ℹ Client built in 8150ms 14:00:17
ℹ Building server... 14:00:17
✔ Server built in 1899ms 14:00:19
ℹ Nitro preset is server 14:00:19
ℹ Cleaning up .output 14:00:19
start Generating public... 14:00:19
✔ Generated public .output/public 14:00:19
start Building server... 14:00:19
start Writing server bundle... 14:00:23
✔ Server built 14:00:23
├─ .output/server/index.mjs (69.6 kB) (23.7 kB gzip)
├─ .output/server/chunks/nitro/static.mjs (11.1 kB) (2.92 kB gzip)
├─ .output/server/chunks/index.mjs (392 kB) (90.3 kB gzip)
├─ .output/server/chunks/app/vue3.mjs (284 B) (200 B gzip)
├─ .output/server/chunks/app/server.mjs (240 kB) (55.2 kB gzip)
├─ .output/server/chunks/app/render.mjs (23 kB) (6.41 kB gzip)
└─ .output/server/chunks/app/client.manifest.mjs (11.4 kB) (1.43 kB gzip)
Σ Total size: 1.68 MB (460 kB gzip)
ℹ You can preview this build using nuxi preview
但是当我运行服务器时,代理没有激活。由于缺乏文档,我认为addServerMiddleware
工作方式addPLugin
与@nuxt/kit
工作方式相同。任何想法我做错了什么?