Replies: 4 comments 1 reply
-
There is another possibility on how to deal with this: just introduce setting for |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
The It's better to use the web callback function to resolve the event import {defineEventHandler, IncomingMessage} from "h3";
import Proxy from 'http-proxy';
const proxy = Proxy.createProxyServer({
target: 'http://jsonplaceholder.typicode.com/',
changeOrigin: true,
ws: true
});
export default defineEventHandler(event => {
if (event.req.url.startsWith('/api/')) {
event.req.url = event.req.url.replace(/^\/api/, '');
return new Promise((resolve, reject) => {
proxy.web(event.req, event.res, {}, (error) => {
if (error) return reject(error)
resolve()
});
});
}
}); If you want to manage the response, you may want to switch the |
Beta Was this translation helpful? Give feedback.
-
Hi, this is work for me // server/api/[...path].ts
import { defineEventHandler, proxyRequest } from 'h3';
export default defineEventHandler((event) => {
const { url } = event.node.req;
if (!url) return;
// your target domain
const domain = 'http://localhost:3000';
return proxyRequest(event, `${domain}${url}`);
}); |
Beta Was this translation helpful? Give feedback.
-
Is there any proxy module that will work with Nitro's global
$fetch
and when that$fetch
is called from SSR context?Right now simple adaptation of
http-proxy
module works with non-SSR requests, but stucks in infinite loop when SSR fetch request is made:end
callback is never getting fired. I suppose that req/res objects are mocked somehow in SSR context, and this mock is not compatible with http-proxy module.Right now this issue makes
$fetch
completely useless (and harmful because it pollutes global namespace) thing inside Nuxt 3, because you will never get any external data with it.Beta Was this translation helpful? Give feedback.
All reactions