koa2能联oracle吗,在koa2应用程序中启用http2stream

您可以使用ctx.res中的流(这是原始节点响应),如下所示:

ctx.res.stream

工作示例:带有http/2的Koa2-这个文件在

public

文件夹(文件名在此硬编码)并通过流发送(然后应该是

http2stream

)中。只是打字

https://localhost:8080/file

在浏览器中。你需要放一个文件

thefile.html

./public

以下内容:

'use strict';

const fs = require('fs');

const http2 = require('http2');

const koa = require('koa');

const app = new koa();

const options = {

key: fs.readFileSync('./key.pem'),

cert: fs.readFileSync('./cert.pem'),

passphrase: 'test'

};

function getFile(path) {

const filePath = `${__dirname}/public/${path}`;

try {

const content = fs.openSync(filePath, 'r');

const contentType = 'text/html';

return {

content,

headers: {

'content-type': contentType

}

};

} catch (e) {

return null;

}

}

// response

app.use(ctx => {

if (ctx.request.url === '/file') {

const file = getFile('thefile.html');

ctx.res.stream.respondWithFD(file.content, file.headers);

} else {

ctx.body = 'OK' ;

}

});

const server = http2.createSecureServer(options, app.callback());

console.log('Listening on port 8080');

server.listen(8080);

希望能帮上忙

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值