当前位置: 首页 > news >正文

nodejs实现http与https服务;同时处理proxy代理的解决方案

// nodejs服务提供的http协议示例
const http = require('http');
const server = http.createServer((req, res) => {res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });res.end(Date.now() + ' == > http访问成功8080')
});
server.listen(8080, () => {console.log('服务已开启');
})
// nodejs服务提供的https协议示例
const https = require('https');
const fs = require('fs');
const path = require('path');
const options = {key: fs.readFileSync(path.join(__dirname, './key.pem')),cert: fs.readFileSync(path.join(__dirname, './cert.pem')),
};
const server = https.createServer(options, (req, res) => {res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });res.end('https访问8081成功 == > ' + Date.now())
});
server.listen(8081, () => {console.log('服务已开启');
})
// 正向代理 :如axios,http.proxy 用户直到,类似于梯子
var http = require('http');
var httpProxy = require('http-proxy')// 创建代理服务器
let proxy = httpProxy.createProxyServer()let server = http.createServer((req, res) => {proxy.web(req, res, {target: 'http://localhost:8080',// target: 'https://localhost:8081', // 并不能代理https})
})server.listen(3000)
// server启动成功
server.on('listening', () => {console.log('http启动完成')
})// 关闭HTTP服务器server.on('close', () => {console.log('服务器关闭')
})
// 反向代理 :解决用户请求的,用户不知道
let httpProxy = require('http-proxy')
let https = require('https');
const fs = require('fs');
const path = require('path');const options = {key: fs.readFileSync(path.join(__dirname, './key.pem')),cert: fs.readFileSync(path.join(__dirname, './cert.pem')),
};// 这是我们配置的域名,我们可以访问这些域名,拿到对应的结果
let hosts = {'as.cc': 'http://localhost:8080',// 'as.com': 'https://localhost:8081',// 也不支持https
}// 创建代理服务器
let proxy = httpProxy.createProxyServer()let server = https.createServer(options, (req, res) => {// 拿到host 访问对应的服务器let host = req.headers['host'].split(':')[0]console.log(666.789, host, hosts[host])proxy.web(req, res, {target: hosts[host] || 'https://localhost:8081'})
})server.listen(3001)
// server启动成功
server.on('listening', () => {console.log('https启动完成')
})// 关闭HTTPS服务器
server.on('close', () => {console.log('服务器关闭')
})
// # nodejs原生实现转发http请求,方案一
const http = require("http");
const server = http.createServer();server.on("request", (req, res) => {var { connection, host, ...originHeaders } = req.headers;var options = {"method": req.method,"hostname": "localhost","port": "8080","path": req.url,"headers": { originHeaders }}//接收客户端发送的数据var p = new Promise((resolve, reject) => {let postbody = [];req.on("data", chunk => {postbody.push(chunk);})req.on('end', () => {let postbodyBuffer = Buffer.concat(postbody);resolve(postbodyBuffer)})});//将数据转发,并接收目标服务器返回的数据,然后转发给客户端p.then((postbodyBuffer) => {let responsebody = []var request = http.request(options, (response) => {response.on('data', (chunk) => {responsebody.push(chunk)})response.on("end", () => {responsebodyBuffer = Buffer.concat(responsebody)res.setHeader('Content-Type', 'text/html;charset=utf-8');res.end(responsebodyBuffer);})})// 使用request的write方法传递请求体request.write(postbodyBuffer)// 使用end方法将请求发出去request.end();})
});
server.listen(3002, () => {console.log("runnng3002");
})
// # nodejs原生实现转发http请求,方案二
const http = require('http');
const server = http.createServer((req, res) => {res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });const options = {hostname: 'localhost',port: 8080,path: req.url,method: req.method};const proxyReq = http.request(options, (proxyRes) => {proxyRes.on('data', (chunk) => {res.write(chunk);});proxyRes.on('end', () => {res.end();});});proxyReq.on('error', (e) => {console.error(`请求遇到问题: ${e.message}`);});req.on('data', (chunk) => {proxyReq.write(chunk);});req.on('end', () => {proxyReq.setHeader('Content-Type', 'text/html;charset=utf-8');proxyReq.end();});
});server.listen(3003, () => {console.log('服务器正在监听3003端口');
});
// # nodejs原生实现转发https请求,方案一
const fs = require('fs');
const path = require('path');
let http = require('http');
let https = require('https');const proxyoptions = {key: fs.readFileSync(path.join(__dirname, './key.pem')),cert: fs.readFileSync(path.join(__dirname, './cert.pem')),
};const server = https.createServer(proxyoptions);server.on("request", (req, res) => {var { connection, host, ...originHeaders } = req.headers;var options = {"method": req.method,// 随表找了一个网站做测试,被代理网站修改这里"hostname": "localhost","port": "8080","path": req.url,"headers": { originHeaders }}//接收客户端发送的数据var p = new Promise((resolve, reject) => {let postbody = [];req.on("data", chunk => {postbody.push(chunk);})req.on('end', () => {let postbodyBuffer = Buffer.concat(postbody);resolve(postbodyBuffer)})});//将数据转发,并接收目标服务器返回的数据,然后转发给客户端p.then((postbodyBuffer) => {let responsebody = []var request = http.request(options, (response) => {response.on('data', (chunk) => {responsebody.push(chunk)})response.on("end", () => {responsebodyBuffer = Buffer.concat(responsebody)res.setHeader('Content-Type', 'text/html;charset=utf-8');res.end(responsebodyBuffer);})})// 使用request的write方法传递请求体request.write(postbodyBuffer)// 使用end方法将请求发出去request.end();})
});
server.listen(3004, () => {console.log("runnng3004");
})
// # nodejs原生实现转发https请求,方案一
const fs = require('fs');
const path = require('path');
let http = require('http');
let https = require('https');const proxyoptions = {key: fs.readFileSync(path.join(__dirname, './key.pem')),cert: fs.readFileSync(path.join(__dirname, './cert.pem')),
};const server = https.createServer(proxyoptions, (req, res) => {res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });const options = {hostname: 'localhost',port: 8080,path: req.url,method: req.method};const proxyReq = http.request(options, (proxyRes) => {proxyRes.on('data', (chunk) => {res.write(chunk);});proxyRes.on('end', () => {res.end();});});proxyReq.on('error', (e) => {console.error(`请求遇到问题: ${e.message}`);});req.on('data', (chunk) => {proxyReq.write(chunk);});req.on('end', () => {proxyReq.setHeader('Content-Type', 'text/html;charset=utf-8');proxyReq.end();});
});server.listen(3004, () => {console.log('服务器正在监听3004端口');
});

相关文章:

nodejs实现http与https服务;同时处理proxy代理的解决方案

// nodejs服务提供的http协议示例 const http require(http); const server http.createServer((req, res) > {res.writeHead(200, { Content-Type: text/html;charsetutf8 });res.end(Date.now() > http访问成功8080) }); server.listen(8080, () > {console.lo…...

C# WPF ListBox 动态显示图片

前言 最近在和其他软件联合做一个本地图片选择传输功能,为此希望图片能够有序的呈现在客户端,简单的实现了一下功能,通过Mvvm模式进行呈现,过程简单通俗,话不多说直接上图。 处理过程 前台代码 你只需要粘贴到你的前台…...

游戏如何防御DDOS流量攻击呢,用游戏盾真的有用么?

针对在线游戏行业来说,DDoS(分布式拒绝服务)攻击是一种极具破坏性的威胁。DDoS攻击可能导致游戏服务器不可用,严重影响游戏体验和运营。为了解决这一问题,游戏盾作为一种专门为游戏行业设计的安全解决方案,…...

vue项目引入antDesignUI组件

快速安装ant-design-vue并配置,vue2.0 antDesign1.7.8 第一步:安装ant-deisgn-vue 1.7.8 npm install ant-design-vue1.7.8 --save第二步:配置package.json文件,将依赖写入后,npm install 安装依赖 "dependenc…...

非结构化数据库-MinIO基本集成

是什么 MinIO 是一个高性能的分布式对象存储服务,适合存储非结构化数据,如图片,音频,视频,日志等。对象文件最大可以达到5TB。 安装启动 mkdir -p /usr/local/minio cd /usr/local/minio# 下载安装包 wget https:/…...

Etcd备份及恢复

一、Etcd数据备份 1、备份命令 [rootlocalhost ~]# export ETCDCTL_API3 [rootlocalhost ~]# /data/etcd-3.4.9/bin/etcdctl --endpoints10.2.20.108:2379 snapshot save etcd-date "%Y-%m-%d_%H-%M-%S".snapshot 2、备份完成后会在当前目录生成备份文件 [rootlo…...

使用JavaMail发送邮件时嵌入公司logo图片

使用JavaMail发送邮件时嵌入公司logo图片 第一种方式:img标签和logo图片链接第二种方式:使用img标签和图片base64字符串第三种方式(推荐):将logo当做附件一起发送并设置ContentID,再使用img标签&#xff0c…...

注解 @Async

注解 Async 1. 注解由来: Async 是 Spring 框架提供的注解,用于将方法标记为异步执行。通过使用 Async 注解,可以告知 Spring 在调用被注解的方法时,使用新的线程或线程池进行异步执行。 2. 注解示例: import org.s…...

Python“牵手”lazada商品评论数据采集方法,lazadaAPI申请指南

lazada平台API接口是为开发电商类应用程序而设计的一套完整的、跨浏览器、跨平台的接口规范,lazadaAPI接口是指通过编程的方式,让开发者能够通过HTTP协议直接访问lazada平台的数据,包括商品信息、店铺信息、物流信息等,从而实现la…...

微信小程序通用字体代码

下面是一个简单的微信小程序通用字体代码示例: // 在app.wxss中设置全局字体样式 import ./styles/fonts.wxss;// 在fonts.wxss中定义字体样式 font-face {font-family: CustomFont;src: url(font.ttf) format(truetype); }// 在page.wxss中使用自定义字体样式 .cus…...

LVS负载均衡DR模式

在LVS(Linux Virtual Server)负载均衡中的DR(Direct Routing)模式下,数据包的流向如下: 客户端发送请求到负载均衡器(LVS)的虚拟IP(VIP)。负载均衡器&#x…...

ArcGIS Pro基础入门、制图、空间分析、影像分析、三维建模、空间统计分析与建模、python融合、案例全流程科研能力提升

目录 第一章 入门篇 GIS理论及ArcGIS Pro基础 第二章 基础篇 ArcGIS数据管理与转换 第三章 数据编辑与查询、拓扑检查 第四章 制图篇 地图符号与版面设计 第五章 空间分析篇 ArcGIS矢量空间分析及应用 第六章 ArcGIS栅格空间分析及应用 第七章 影像篇 遥感影像处理 第八…...

​ Spring Clould 配置中心 - Nacos

视频地址:微服务(SpringCloudRabbitMQDockerRedis搜索分布式) Nacos配置管理-Nacos实现配置管理(P24、P25) Nacos除了可以做注册中心,同样可以做配置管理来使用。 当微服务部署的实例越来越多&#xff0c…...

1609.奇偶数

目录 一、题目 二、代码 三、完整测试代码 一、题目 1609. 奇偶树 - 力扣(LeetCode) 二、代码 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0),…...

c++--异常

1.什么是异常 对于C语言来说,处理错误的机制有: 1.终止程序:如assert,缺陷,如发生内存错误,除0之外发生程序终止,用户无法接受。 2.返回错误码:对于大型程序来说,需要…...

ArcGIS 利用cartogram插件制作变形地图

成果图 注:本图数据并不完全对,只做为测试用例 操作 首先需要下载一个插件cartogram 下载地址在这里 https://www.arcgis.com/home/item.html?idd348614c97264ae19b0311019a5f2276 下载完毕之后解压将Cartograms\HelpFiles下的所有文件复制到ArcGIS…...

Mybatis批量插入方式有哪些

MyBatis的批量插入有多种写法&#xff0c;下面我将列出一些常见的批量插入写法 方式列表 使用XML配置文件进行批量插入&#xff1a;在XML映射文件中使用<insert>标签&#xff0c;并通过foreach标签迭代批量数据&#xff0c;然后在SQL语句中使用VALUES关键字。使用Java注…...

前端框架学习-React(一)

React 应用程序是由组件组成的。 react 程序是用的jsx语法&#xff0c;使用这种语法的代码需要由babel进行解析&#xff0c;解析成js代码。 jsx语法&#xff1a; 只能返回一个根元素 所有的标签都必须闭合&#xff08;自闭和或使用一对标签的方式闭合&#xff09; 使用驼峰式…...

Android Studio实现解析HTML获取图片URL将图片保存到本地

目录 效果activity_main.xmlMainActivityImageItemImageAdapter 效果 项目本来是要做成图片保存到手机然后读取数据后瀑布流展示&#xff0c;但是有问题&#xff0c;目前只能做到保存到手机 activity_main.xml <?xml version"1.0" encoding"utf-8"?…...

单例模式的理论与实践

本文实践代码仓库&#xff1a;https://github.com/goSilver/my_practice 文章目录 一、定义二、作用三、实现3.1 饿汉式3.2 懒汉式3.3 双重检查3.4 静态内部类3.5 枚举 四、总结4.1 单例存在哪些问题&#xff1f;4.2 单例有什么替代解决方案&#xff1f; 一、定义 单例模式是一…...

深入了解MongoDB:灵活的文档型数据库与应用案例

什么是MongoDB ? MongoDB 是由C语言编写的&#xff0c;是一个基于分布式文件存储的开源数据库系统。 在高负载的情况下&#xff0c;添加更多的节点&#xff0c;可以保证服务器性能。 MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB 将数据存储为一个…...

【HarmonyOS北向开发】-01 HarmonyOS概述

飞书原文链接-【HarmonyOS北向开发】-01 HarmonyOS概述https://fvcs2dhq8qs.feishu.cn/docx/TDf2d2KMaoPSUUxnvg2cASDdnCe?fromfrom_copylink...

Node.js入门

安装 前往官网下载即可&#xff1a;https://nodejs.org/zh-cn 安装之后检查是否成功并查看版本&#xff0c;winr --> 输入cmd --> 确认 --> 进入命令提示符窗口 --> 输入 node -v --> 出现以下就代表成功了&#xff0c;这也是node的版本号 什么是Node.js Nod…...

指针、数组、sizeof、strlen相关知识与练习题目

目录 前提回顾&#x1f50d;&#xff1a; 关于一维数组&#x1f92e;&#xff1a; 关于二维数组&#x1f600;&#xff1a; sizeof与strlen&#x1f415;&#xff1a; sizeof&#x1f3c0;&#xff1a; strlen&#x1f413;&#xff1a; 相关练习&#x1f4da;&#xff1a…...

分类预测 | MATLAB实现WOA-CNN-BiLSTM-Attention数据分类预测

分类预测 | MATLAB实现WOA-CNN-BiLSTM-Attention数据分类预测 目录 分类预测 | MATLAB实现WOA-CNN-BiLSTM-Attention数据分类预测分类效果基本描述程序设计参考资料 分类效果 基本描述 1.MATLAB实现WOA-CNN-BiLSTM-Attention数据分类预测&#xff0c;运行环境Matlab2023b及以上…...

MyBatis动态SQL:打造灵活可变的数据库操作

目录 if标签trim标签where标签set标签foreach标签 动态SQL就是根据不同的条件或需求动态地生成查询语句&#xff0c;比如动态搜索条件、动态表或列名、动态排序等。 if标签 在我们填写一些信息时&#xff0c;有些信息是必填字段&#xff0c;有的则是非必填的&#xff0c;这些…...

nginx代理请求到内网不同服务器

需求&#xff1a;之前用的是frp做的内网穿透&#xff0c;但是每次电脑断电重启&#xff0c;路由或者端口会冲突&#xff0c;现在使用汉土云盒替换frp。 需要把公网ip映射到任意一台内网服务器上&#xff0c;然后在这台内网服务器上用Nginx做代理即可访问内网其它服务器&#xf…...

【C# 基础精讲】文件读取和写入

文件读取和写入是计算机程序中常见的操作&#xff0c;用于从文件中读取数据或将数据写入文件。在C#中&#xff0c;使用System.IO命名空间中的类来进行文件读写操作。本文将详细介绍如何在C#中进行文件读取和写入&#xff0c;包括读取文本文件、写入文本文件、读取二进制文件和写…...

设计模式——经典单例

0、核心要素 // 构造、析构函数私有化&#xff08;一个进程只允许一个对象存在&#xff09; // 对象私有化、静态化&#xff08;因为接口静态函数&#xff09; // 对象调用接口静态化&#xff08;因为静态函数脱离了类对象&#xff0c;可以直接调用&#xff09; 一、懒汉 唯…...

【HarmonyOS】鸿蒙应用获取华为帐号手机号码步骤(API7及以下)

【写在前面】 本文主要介绍使用API7及以下版本开发HarmonyOS应用时&#xff0c;通过华为帐号SDK和云侧接口获取手机号码的主要开发步骤&#xff0c;注意&#xff1a;开发过程中集成的华为帐号SDK仅支持API7及以下版本的HarmonyOS应用。 【前提准备】 1、HarmonyOS应用已申请获…...