Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
api代理模块,解决跨域、权限问题
远程需要被代理转发的服务:
http://localhost:7001/api/aaa // 视频服务接口
http://localhost:7001/api/c/* // 视频服务接口
http://192.168.1.1:7001/api/bbb // 音乐服务接口
http://localhost:7001/ws/* // 聊天服务接口 (使用websocket)
// router.js
const app = require('./app');
const Proxy = require('hc-proxy');
const proxyInstance = new Proxy({
service: {
video: {
endpoint: 'http://localhost:7001/',
client: 'http',
api: [
'/api/aaa',
'/api/c/*'
]
},
music: {
endpoint: 'http://192.168.1.1:7001/',
client: 'http',
api: [
'/api/bbb'
]
}
},
headers: [
'x-csrf-token',
'X-Operator'
]
});
proxyInstance.setProxyPrefix('/api/proxy');
module.exports = function (router) {
proxyInstance.mount(router, app);
};
访问地址为 ${honeycomb服务地址:honeycomb服务端口/honeycomb的prefix} + ${proxyPrefix} + ${被代理服务名} + ${具体api}
1. 视频服务: curl http://localhost:8001/example/api/proxy/video/api/aaa => http://localhost:7001/api/aaa
2. 视频服务: curl http://localhost:8001/example/api/proxy/video/api/c/somewhat?xxx=123 => http://localhost:7001/api/c/somewhat?xxx=123
3. 音乐服务: curl http://localhost:8001/example/api/proxy/music/api/bbb => http://192.168.1.1:7001/api/bbb
// router.js
const app = require('./app');
const Proxy = require('hc-proxy');
const proxyInstance = new Proxy({
service: {
music: {
endpoint: 'http://192.168.1.1:7001/',
client: 'http',
api: [
'/api/bbb'
]
},
chat: {
endpoint: 'http://localhost:7001/',
client: 'websocket',
api: [
'/ws/*'
]
}
},
headers: [
'x-csrf-token',
'X-Operator'
]
});
proxyInstance.setProxyPrefix('/api/proxy');
module.exports = function (router) {
proxyInstance.mount(router, app);
};
访问地址为 ${honeycomb服务地址:honeycomb服务端口/honeycomb的prefix} + ${proxyPrefix} + ${被代理服务名} + ${具体api}
1. 音乐服务: curl http://localhost:8001/example/api/proxy/music/api/bbb => http://192.168.1.1:7001/api/bbb
2. 聊天服务: curl http://localhost:8001/example/api/proxy/chat/ws/somewhat => http://localhost:7001/ws/somewhat // 支持websocket
// router.js
const app = require('./app');
const Proxy = require('hc-proxy');
const proxyInstance = new Proxy({
service: {
music: {
endpoint: 'http://192.168.1.1:7001/',
client: 'http',
api: [
{
path: '/api/upload',
file: true
},
{
path: '/api/upload_limited',
file: {
maxFileSize: 100 // 100B
}
}
]
}
},
headers: [
'x-csrf-token',
'X-Operator'
]
});
proxyInstance.setProxyPrefix('/api/proxy');
module.exports = function (router) {
proxyInstance.mount(router, app);
};
1. 上传文件1: curl http://localhost:8001/example/api/proxy/music/api/upload => http://192.168.1.1:7001/api/upload
2. 上传文件2: curl http://localhost:8001/example/api/proxy/music/api/upload_limited => http://192.168.1.1:7001/api/upload_limited
约定:
options.service 详情
{
${serviceCode}: {
endpoint: ${endpoint},
accessKeyId: ${accessKeyId},
accessKeySecret: ${accessKeySecret},
headerExtension: ${headerExtension},
headers: ${headers},
client: ${client},
timeout: ${timeout},
useQuerystringInDelete: ${useQuerystringInDelete}, // 只有 appClient / urllib 模式有效
urllibOption: ${urllibOption}, // 只有 appClient / urllib 模式有效
api: [
${apiString},
{
path: ${path},
route: ${route},
method: ${method},
timeout: ${apiTimeout},
defaultQuery: ${defaultQuery},
beforeRequest: ${beforeRequest}, // TODO
useQuerystringInDelete: ${useQuerystringInDelete},
urllibOption: ${urllibOption}
}
]
}
}
通用配置:
client=[appClient/serviceClient]的专属配置
options.headers 用于声明proxy需要转发的header。 默认情况下,proxy不转发客户端过来的header,只有在proxyHeaders中配置的header才会被转发。
setProxyPrefix方法用于指定 hc-proxy 挂载在代理服务上的总前缀
如: 默认 proxyPrefix = /api/proxy 则: 所有请求远端服务的请求,格式为 ${localHttpServer}/api/proxy/${serviceCode}/${remoteApi}
将proxy的配置挂在到代理服务。
'use strict';
const app = require('./app');
const Proxy = require('hc-proxy');
const proxyInstance = new Proxy({
service: {
otm: {
endpoint: 'http://dev.dtboost.biz.aliyun.test/otm_v2', // 自动截取最后的'/'
client: 'appClient', // 默认appClient
timeout: 10000, // 默认60000
api: [
'/api/*', // 支持 * 代理某个path下的所有api
'/otm_v2/api/entities/list', // 代理这个 url 的 GET POST PUT DELETE 方法
{path: '/otm_v2/api/entities/list', method: 'GET'}, // 只代理 GET 方法
{path: '/otm_v2/api/entities/list', client: 'appClient'}, // 显式指定 method
{path: '/otm_v2/api/schemas', method: ['GET', 'POST']}, // 只支持 GET POST 方法
{host: 'http://taobao.com/api', path: '/tag_factory_v2/api/:id', method: 'GET'}, // 不同域时,指定host
{path: 'http://taobao.com/api', route: '/api/proxy/taobao_api'}, // 指定route的代理,不指定route时,所有代理接口请求 /<-app_name->/api/proxy 获得代理
'/otm_v2/api/tags/query_tags_entity_pagenum', //
{path: '/otm_v2/api/schemas/:id', route: '/api/proxy/schemas/:id'} // 带参数的url,保证route和url中的param一致,也可以不填route
]
},
websocket: {
endpoint: 'http://dev.dtboost.biz.aliyun.test/websocket', // 远程的websocket地址
client: 'websocket', // 选择websocket作为连接的client
api: [
'/ws' // 配置连接路径,不在的路径返回404
]
}
},
headers: [
'x-csrf-token',
'X-Operator'
]
});
proxyInstance.setProxyPrefix('/api/proxy');
module.exports = function (router) {
proxyInstance.mount(router, app);
};
在命令行启动命令前加入DEBUG=hc-proxy
// 以启动命令为 honeycomb start 为例
DEBUG=hc-proxy honeycomb start
// ...
// hc-proxy [GET] /api/proxy/urllib_proxy/urllib -> http://localhost:58062/urllib +0ms
// hc-proxy [POST] /api/proxy/urllib_proxy/urllib -> http://localhost:58062/urllib +3ms
// hc-proxy [DELETE] /api/proxy/urllib_proxy/urllib -> http://localhost:58062/urllib +1ms
// hc-proxy [PUT] /api/proxy/urllib_proxy/urllib -> http://localhost:58062/urllib +0ms
// ...
FAQs
honeycomb api proxy express middleware.
The npm package hc-proxy receives a total of 4 weekly downloads. As such, hc-proxy popularity was classified as not popular.
We found that hc-proxy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.