h3-proxy
A proxy event handler for h3, using proxyRequest
.
Features
Installation
$ pnpm add h3-proxy
$ yarn add h3-proxy
$ npm i h3-proxy
Basic usage
import { createServer } from 'node:http'
import { createApp, eventHandler, toNodeListener } from 'h3'
import { createProxyEventHandler } from 'h3-proxy'
const app = createApp()
const port = process.env.PORT || 3000
const proxyEventHandler = createProxyEventHandler({
target: `http://127.0.0.1:${port}`,
pathRewrite: {
'^/api': '',
},
pathFilter: ['/api/**'],
})
app.use(eventHandler(proxyEventHandler))
app.use(
'/test',
eventHandler(() => 'Hello world!')
)
createServer(toNodeListener(app)).listen(port)
Multiple proxy targets usage
Create multiple proxy h3 event handler middleware.
import { createServer } from 'node:http'
import { createApp, eventHandler, toNodeListener } from 'h3'
import { createProxyEventHandler } from 'h3-proxy'
const app = createApp()
const port = process.env.PORT || 3000
const proxyEventHandler1 = createProxyEventHandler({
target: `http://127.0.0.1:${port}`,
pathRewrite: {
'^/api': '',
},
pathFilter: ['/api/**'],
})
const proxyEventHandler2 = createProxyEventHandler({
target: `http://127.0.0.1:${port}/other-api-module`,
pathRewrite: {
'^/other-api': '',
},
pathFilter: ['/other-api/**'],
})
app.use(eventHandler(proxyEventHandler1))
app.use(eventHandler(proxyEventHandler2))
app.use(
'/test',
eventHandler(() => 'Hello world!')
)
app.use(
'/other-api-module/some/path',
eventHandler(() => 'Hello other API module!')
)
createServer(toNodeListener(app)).listen(port)
- For
proxyEventHandler1
, The result of proxy request is as follows:
/api/test
-> http://127.0.0.1:3000/test
- For
proxyEventHandler2
, The result of proxy request is as follows:
/other-api/some/path
-> http://127.0.0.1:3000/other-api-module/some/path
Or, using Array type options to define multiple proxy targets (added in v1.11.0).
const proxyEventHandler = createProxyEventHandler([
{
},
{
}
])
APIs
createProxyEventHandler
Create a h3
event handler that can handle proxy requests.
const proxyEventHandler = createProxyEventHandler({
})
Options
Key | Type | Required | Default value | Description |
---|
target | string | true | undefined | Proxy target address, including protocol, host and port. url string to be parsed with the node:url module |
pathFilter | string, string[], glob, glob[], Function | false | undefined | Narrow down which requests should be proxied. |
pathRewrite | object/Function | false | undefined | Rewrite target's url path. Object-keys will be used as RegExp to match paths. |
configureProxyRequest | Function | false | undefined | Configure options of proxyRequest . More details see built-in util proxyRequest of h3 |
proxyRequestMethod Added in v1.13.0 | Function | false | undefined | Customize proxyRequest of h3 method. |
enableLogger | boolean | false | true | Whether to enable logger which is created by consola. |
loggerOptions | ConsolaOptions | false | {} | Configure the options of consola. |
changeOrigin | boolean | false | false | Whether to changes the origin of the host header to the target URL |
pathFilter
:warning: TIPS, In multiple path matching, you cannot use string paths and wildcard paths together.
-
custom matching
For full control you can provide a custom function to determine which requests should be proxied or not.
const pathFilter = function (path, req) {
return path.match(/^\/api/) && req.method === 'GET';
};
const apiProxy = createProxyEventHandler({
target: 'http://www.example.org',
pathFilter: pathFilter,
});
pathRewrite
Rewrite target's url path. Object-keys will be used as RegExp to match paths.
pathRewrite: {'^/old/api' : '/new/api'}
pathRewrite: {'^/remove/api' : ''}
pathRewrite: {'^/' : '/basepath/'}
pathRewrite: function (path, req) { return path.replace('/api', '/base/api') }
pathRewrite: async function (path, req) {
const should_add_something = await httpRequestToDecideSomething(path);
if (should_add_something) path += "something";
return path;
}
configureProxyRequest
For the return value, Please refer to the source code of proxyRequest of h3.
createProxyEventHandler({
configureProxyRequest(event) {
return {}
}
})
Framework Supports
Nuxt
Add a server middleware.
import { createProxyEventHandler } from 'h3-proxy'
export default defineEventHandler(createProxyEventHandler({
}))