Errors in `yarn dev` (server/middleware/apiProxy.ts file)
Closed this issue · 13 comments
I think the update version have some problems. (in server/middleware/apiProxy.ts)
I have some errors when I run yarn dev
and my code is this.
## current version
import { createProxyMiddleware } from 'http-proxy-middleware'
export default defineEventHandler(async (event) => {
await new Promise((resolve, reject) => {
createProxyMiddleware({
target: process.env.SERVER_DOMAIN,
pathFilter: '/api',
})(event.node.req, event.node.res, (err) => {
if (err)
reject(err)
else
resolve(true)
})
})
})
but when I run with this code everything is good.
## previous version
const PayloadMethods = new Set(["PATCH", "POST", "PUT", "DELETE"]);
export default defineEventHandler(async (event) => {
// @ts-ignore
if (event.node.req.url.startsWith('/api/')) {
// TODO: fix fetch failed
const target = (process.env.SERVER_DOMAIN || 'http://localhost:8000') + event.node.req.url
// Method
const method = getMethod(event)
// Body
let body;
if (PayloadMethods.has(method)) {
body = await readRawBody(event).catch(() => undefined);
}
// Headers
const headers = getProxyRequestHeaders(event);
if (method === 'DELETE') {
delete headers['content-length']
}
return sendProxy(event, target, {
sendStream: event.node.req.url === '/api/conversation/',
fetchOptions: {
headers,
method,
body,
},
});
}
})
yes i did
What's the error content?
This is my error.
500
[HPM] Missing "target" option. Example: {target: "http://www.example.org"}
at verifyConfig (./node_modules/http-proxy-middleware/dist/configuration.js:7:15)
at new HttpProxyMiddleware (./node_modules/http-proxy-middleware/dist/http-proxy-middleware.js:124:42)
at createProxyMiddleware (./node_modules/http-proxy-middleware/dist/index.js:20:28)
at ./.nuxt/dev/index.mjs:541:5
at new Promise ()
at Object.handler (./.nuxt/dev/index.mjs:540:9)
at Object.handler (./node_modules/h3/dist/index.mjs:1247:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async toNodeHandle (./node_modules/h3/dist/index.mjs:1322:7)
at async Object.ufetch [as localFetch] (./node_modules/unenv/runtime/fetch/index.mjs:9:17)
I run django server(localhost:8000) but still have the same error
(my yarn version is v1.22.19
and node version is v16.20.0
)
I understand what you mean and it runs well! Thanks for your help 😃
请问.env 文件在哪里呢
you can't see .env file on local. you need to use text editor(ex vscode) or if you can't find either, you have to make the file
I can not find the template.env ,so I create a new .env file . the content is below:
SERVER_DOMAIN = "http://127.0.0.1:8000/"
anything else?
that's all 👍
you can't see .env file on local. you need to use text editor(ex vscode) or if you can't find either, you have to make the file
Where should the.env file be placed? The root directory of the project?
yes
yes
Have you attempted to start the project with the command node .output/server/index.mjs
after yarn build
to deploy the project? It seems the .env file cannot be read.