使用 nodejs 中的 http 模块实现几个超实用的工具,web后端开发工程师

if (index >= 0) {

const query = req.url.slice(index);

const ss = new URLSearchParams(query);

const timeout = ss.get(‘timeout’);

const type = ss.get(‘type’);

if (timeout && Number(timeout)) {

return setTimeout(() => {

if (type === ‘json’) {

res.writeHead(200, { ‘Content-Type’: ‘application/json’ });

res.end(JSON.stringify({ code: 0, msg: ‘hello world’ }));

} else if (type === ‘image’) {

// 输出本地一个图片

} else {

res.end(delay ${timeout}ms response);

}

}, Number(timeout));

}

}

res.end(‘hello world!’);

})

.listen(port, ip);

想要延迟输出图片时,需要通过数据流的方式读取本地的图片,然后输出到前端:

const stream = fs.createReadStream(‘./img/s.jpg’);

const responseData = []; //存储文件流

if (stream) {

//判断状态

stream.on(‘data’, function (chunk) {

responseData.push(chunk);

});

stream.on(‘end’, function () {

const finalData = Buffer.concat(responseData);

// response.write();

res.writeHead(200, { ‘Content-Type’: ‘image/jpg’ });

res.end(finalData);

});

}

3. 实现接口的中转代理


我们有时会遇到需要的接口存在跨域,或者是内网接口的问题,这时我们就需要通过一个中间层,来对接口进行中转代理,才能正常地访问接口。

无所谓,也有点累

3.1 原生 http 模块来实现

实现接口代理时要注意亮点:

  1. 透传,接收到的所有数据,根据需要尽量都传给代理的接口,例如 cookie,参数等;

  2. 设置跨域头,为了方便前端的访问,我们需要在返回的头部加上 3 个可以跨域的字段;

跨域的方式有很多种,比如 jsonp 也是其中一种,但 cors 跨域是比较好的一种,前端可以有效地控制请求时间和取消请求。

在设置跨域头Access-Control-Allow-Origin时,这里是不建议直接设置成*。一方面是不安全,所有的域名都可以访问;再有就是前端不会再传送 cookie,无法进行一些登录态的校验等。

在设置Access-Control-Allow-Origin之前,我们要先校验下 headers 中的 referer,如果为空或者不满足白名单的要求,则可以直接返回 403。

const allowList = [‘joke.qq.com’, ‘www.qq.com’];

if (!req.headers || !req.headers.referer) {

res.writeHead(403, ‘forbidden’);

res.end(‘403 forbidden’);

return;

}

const { hostname } = new URL(req.headers.referer);

if (!allowList.includes(hostname)) {

res.writeHead(403, ‘forbidden’);

res.end(‘403 forbidden’);

return;

}

满足要求之后,需要将 referer 最后的斜杠/去掉,否则会设置不成功。完成的代码样例如下:

const http = require(‘http’);

const https = require(‘https’);

const ip = process.env.IP || ‘127.0.0.1’;

const port = process.env.PORT || 3001;

http

.createServer((req, res) => {

const allowList = [‘joke.qq.com’, ‘www.qq.com’];

if (!req.headers || !req.headers.referer || allow) {

res.writeHead(403, ‘forbidden’);

res.end(‘403 forbidden’);

return;

}

console.log(‘发起请求’, req.headers);

https

.get(‘https://www.v2ex.com/api/topics/latest.json’, (response) => {

let data = ‘’;

response.on(‘data’, (chunk) => {

data += chunk;

});

response.on(‘end’, () => {

res.setHeader(‘Access-Control-Allow-Origin’, (req.headers.referer || ‘’).replace(//$/, ‘’));

res.setHeader(‘Access-Control-Allow-Methods’, ‘GET, POST’);

res.setHeader(‘Access-Control-Allow-Headers’, ‘X-Requested-With,content-type’);

res.end(data);

});

})

.on(‘error’, (e) => {

console.error(请求遇到问题: ${e.message}, e);

res.end(‘error’);

});

})

.listen(port, ip);

console.log(server has started at ${ip}:${port});

3.2 proxy 代理组件

若需要代理更多的接口,或者路径是从前端传过来的,我们自己倒是也可以实现,不过还有更方便的 proxy 代理组件了。

我养你

这里我们用 http-proxy 组件来实现:

const http = require(‘http’);

const httpProxy = require(‘http-proxy’);

const ip = process.env.IP || ‘127.0.0.1’;

const port = process.env.PORT || 3000;

const proxy = httpProxy.createProxyServer({

target: ‘https://www.v2ex.com’, // 代理的接口地址

changeOrigin: true,

});

http

.createServer((req, res) => {

// 设置跨域头

res.setHeader(‘Access-Control-Allow-Origin’, (req.headers.referer || ‘’).replace(//$/, ‘’));

res.setHeader(‘Access-Control-Allow-Methods’, ‘GET, POST’);

res.setHeader(‘Access-Control-Allow-Headers’, ‘X-Requested-With,content-type’);

// 将请求和响应的对象传给proxy

proxy.web(req, res);

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
img

[外链图片转存中…(img-QHKlaDBm-1710915984330)]
[外链图片转存中…(img-Xuc2IOjO-1710915984331)]
[外链图片转存中…(img-AZBzaHSq-1710915984331)]
[外链图片转存中…(img-nWAYc4S9-1710915984332)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
[外链图片转存中…(img-zh0SJ8nD-1710915984332)]

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值