Node-HTTP-Proxy 开源项目教程
2024-08-10 11:09:32作者:戚魁泉Nursing
项目介绍
Node-HTTP-Proxy 是一个功能全面的 HTTP 代理库,适用于 Node.js。它支持正向代理和反向代理,适用于多种场景,如负载均衡、API 请求转发等。该项目由 http-party 维护,是一个活跃的开源项目,拥有广泛的社区支持和丰富的文档资源。
项目快速启动
安装
首先,确保你已经安装了 Node.js 和 npm。然后,通过 npm 安装 node-http-proxy:
npm install http-proxy
基本使用
以下是一个简单的示例,展示如何创建一个基本的 HTTP 代理服务器:
const http = require('http');
const httpProxy = require('http-proxy');
// 创建代理服务器
const proxy = httpProxy.createProxyServer({});
// 创建 HTTP 服务器
http.createServer(function(req, res) {
// 将请求代理到目标服务器
proxy.web(req, res, { target: 'http://localhost:8080' });
}).listen(3000, () => {
console.log('代理服务器运行在 http://localhost:3000/');
});
应用案例和最佳实践
负载均衡
Node-HTTP-Proxy 可以用于实现简单的负载均衡。以下是一个示例,展示如何将请求分发到多个后端服务器:
const servers = [
'http://localhost:8081',
'http://localhost:8082',
'http://localhost:8083'
];
let current = 0;
http.createServer((req, res) => {
proxy.web(req, res, { target: servers[current] });
current = (current + 1) % servers.length;
}).listen(3000);
API 网关
Node-HTTP-Proxy 也可以用作 API 网关,将不同的请求路由到不同的后端服务:
http.createServer((req, res) => {
if (req.url.startsWith('/api/v1')) {
proxy.web(req, res, { target: 'http://localhost:8081' });
} else if (req.url.startsWith('/api/v2')) {
proxy.web(req, res, { target: 'http://localhost:8082' });
} else {
res.writeHead(404);
res.end();
}
}).listen(3000);
典型生态项目
Express 集成
Node-HTTP-Proxy 可以与 Express 框架集成,提供更高级的路由和中间件功能:
const express = require('express');
const proxy = require('http-proxy').createProxyServer();
const app = express();
app.use('/api', (req, res) => {
proxy.web(req, res, { target: 'http://localhost:8080' });
});
app.listen(3000, () => {
console.log('服务器运行在 http://localhost:3000/');
});
WebSocket 支持
Node-HTTP-Proxy 也支持 WebSocket 代理:
const WebSocket = require('ws');
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});
const wss = new WebSocket.Server({ port: 3000 });
wss.on('connection', (ws) => {
ws.on('message', (message) => {
proxy.ws(ws, { target: 'ws://localhost:8080' });
});
});
通过这些示例和最佳实践,你可以充分利用 Node-HTTP-Proxy 的功能,构建强大的代理服务器和 API 网关。
登录后查看全文
热门项目推荐
相关项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0216
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0138
uni-appA cross-platform framework using Vue.jsJavaScript08
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook03
热门内容推荐
最新内容推荐
项目优选
收起
deepin linux kernel
C
32
16
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
471
465
Ascend Extension for PyTorch
Python
758
968
昇腾LLM分布式训练框架
Python
185
231
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
698
1.4 K
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
878
2.03 K
暂无描述
Dockerfile
780
5.08 K
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
70
22
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
271
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
2.08 K
216