Gathering detailed insights and metrics for h3-proxy
As of June 2024, the npm registry hosts over 2 million packages, making it one of the largest open-source software repositories in the world.
Gathering detailed insights and metrics for h3-proxy
As of June 2024, the npm registry hosts over 2 million packages, making it one of the largest open-source software repositories in the world.
npm install h3-proxy
55.3
Supply Chain
96.9
Quality
79.4
Maintenance
100
Vulnerability
100
License
12 Stars
80 Commits
2 Watching
3 Branches
1 Contributors
Updated on 20 Nov 2024
TypeScript (78.3%)
JavaScript (21.7%)
Cumulative downloads
Total Downloads
Last day
13.2%
737
Compared to previous day
Last week
-1.8%
3,984
Compared to previous week
Last month
-32%
18,090
Compared to previous month
Last year
707.3%
138,095
Compared to previous year
3
1
24
A proxy event handler for h3, using proxyRequest
.
1# pnpm 2$ pnpm add h3-proxy 3 4# yarn 5$ yarn add h3-proxy 6 7# npm 8$ npm i h3-proxy
1import { createServer } from 'node:http' 2import { createApp, eventHandler, toNodeListener } from 'h3' 3import { createProxyEventHandler } from 'h3-proxy' 4 5const app = createApp() 6 7const port = process.env.PORT || 3000 8 9// Define proxy event handler 10const proxyEventHandler = createProxyEventHandler({ 11 target: `http://127.0.0.1:${port}`, 12 pathRewrite: { 13 '^/api': '', 14 }, 15 pathFilter: ['/api/**'], 16 // enableLogger: true 17}) 18 19// Add proxy middlewares 20app.use(eventHandler(proxyEventHandler)) 21 22// Define api routes 23app.use( 24 '/test', 25 eventHandler(() => 'Hello world!') 26) 27 28createServer(toNodeListener(app)).listen(port)
1import { createServer } from 'node:http' 2import { createApp, eventHandler, toNodeListener } from 'h3' 3import { createProxyEventHandler } from 'h3-proxy' 4 5const app = createApp() 6 7const port = process.env.PORT || 3000 8 9// proxy to `http://127.0.0.1:${port}` 10const proxyEventHandler1 = createProxyEventHandler({ 11 target: `http://127.0.0.1:${port}`, // http://127.0.0.1:3000 12 pathRewrite: { 13 '^/api': '', 14 }, 15 pathFilter: ['/api/**'], 16}) 17 18// proxy to other target 19const proxyEventHandler2 = createProxyEventHandler({ 20 target: `http://127.0.0.1:${port}/other-api-module`, 21 pathRewrite: { 22 '^/other-api': '', 23 }, 24 pathFilter: ['/other-api/**'], 25}) 26 27// Add proxy middlewares 28app.use(eventHandler(proxyEventHandler1)) 29app.use(eventHandler(proxyEventHandler2)) 30 31// Define api routes 32app.use( 33 '/test', 34 eventHandler(() => 'Hello world!') 35) 36 37app.use( 38 '/other-api-module/some/path', 39 eventHandler(() => 'Hello other API module!') 40) 41 42createServer(toNodeListener(app)).listen(port)
proxyEventHandler1
, The result of proxy request is as follows:/api/test
-> http://127.0.0.1:3000/test
proxyEventHandler2
, The result of proxy request is as follows:/other-api/some/path
-> http://127.0.0.1:3000/other-api-module/some/path
1const proxyEventHandler = createProxyEventHandler([ 2 { 3 // options 4 }, 5 { 6 // other proxy target options 7 } 8])
Create a h3
event handler that can handle proxy requests.
1const proxyEventHandler = createProxyEventHandler({ 2 // options 3})
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 |
path matching
createProxyEventHandler({...})
- matches any path, all requests will be proxied when pathFilter
is not configured.createProxyEventHandler({ pathFilter: '/api', ...})
- matches paths starting with /api
multiple path matching
createProxyEventHandler({ pathFilter: ['/api', '/ajax', '/someotherpath'], ...})
wildcard path matching
For fine-grained control you can use wildcard matching. Glob pattern matching is done by micromatch. Visit micromatch or glob for more globbing examples.
createProxyEventHandler({ pathFilter: '**', ...})
matches any path, all requests will be proxied.createProxyEventHandler({ pathFilter: '**/*.html', ...})
matches any path which ends with .html
createProxyEventHandler({ pathFilter: '/*.html', ...})
matches paths directly under path-absolutecreateProxyEventHandler({ pathFilter: '/api/**/*.html', ...})
matches requests ending with .html
in the path of /api
createProxyEventHandler({ pathFilter: ['/api/**', '/ajax/**'], ...})
combine multiple patternscreateProxyEventHandler({ pathFilter: ['/api/**', '!**/bad.json'], ...})
exclusion: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.
1/** 2 * @return {Boolean} 3 */ 4const pathFilter = function (path, req) { 5 6 return path.match(/^\/api/) && req.method === 'GET'; 7 8 // TIPS: if you are using it in nuxt-proxy-request 9 // Pls use `new RegExp()` instead. 10 11 // return path.match(new RegExp('^\/api')) && req.method === 'GET'; 12}; 13 14const apiProxy = createProxyEventHandler({ 15 target: 'http://www.example.org', 16 pathFilter: pathFilter, 17});
Rewrite target's url path. Object-keys will be used as RegExp to match paths.
1// rewrite path 2pathRewrite: {'^/old/api' : '/new/api'} 3 4// remove path 5pathRewrite: {'^/remove/api' : ''} 6 7// add base path 8pathRewrite: {'^/' : '/basepath/'} 9 10// custom rewriting 11pathRewrite: function (path, req) { return path.replace('/api', '/base/api') } 12 13// custom rewriting, returning Promise 14pathRewrite: async function (path, req) { 15 const should_add_something = await httpRequestToDecideSomething(path); 16 if (should_add_something) path += "something"; 17 return path; 18}
For the return value, Please refer to the source code of proxyRequest of h3.
1createProxyEventHandler({ 2 // ... 3 // the param `event` is H3Event 4 configureProxyRequest(event) { 5 // return your custom options of proxyRequest 6 7 // eg: specify some request headers 8 // return { 9 // headers: {} 10 // } 11 12 return {} 13 } 14})
h3-proxy
directly in Nuxt.Add a server middleware.
1// ~/server/middleware/proxy.ts 2 3import { createProxyEventHandler } from 'h3-proxy' 4 5export default defineEventHandler(createProxyEventHandler({ 6 // options... 7}))
No vulnerabilities found.
No security vulnerabilities found.
nuxt-proxy-request
A http proxy module for nuxt(3) powered by h3-proxy.
@radya/nuxt-proxy-party
Nuxt HTTP proxy based on H3
nuxt-proxy
Http-proxy middleware for Nuxt 3.
@teamteanpm2024/dignissimos-officiis-tempora
<p align="center"> <img src="https://framerusercontent.com/images/48ha9ZR9oZQGQ6gZ8YUfElP3T0A.png" width="50" height="50" alt="Framer Motion Icon" /> </p> <h1 align="center">Framer Motion</h1> <h3 align="center"> An open source motion library for Reac