koa response image

koa.js

v1.x generator *function

v2.x Async/Await

koajs/examples

https://github.com/koajs/examples

https://github.com/koajs/examples/blob/master/stream-file/app.js


const Koa = require('koa');
const fs = require('fs');
const app = module.exports = new Koa();
const path = require('path');
const extname = path.extname; // try GET /app.js app.use(async function(ctx) {
const fpath = path.join(__dirname, ctx.path);
const fstat = await stat(fpath); if (fstat.isFile()) {
ctx.type = extname(fpath);
ctx.body = fs.createReadStream(fpath);
}
}); if (!module.parent) app.listen(3000); /**
* thunkify stat
*/ function stat(file) {
return new Promise(function(resolve, reject) {
fs.stat(file, function(err, stat) {
if (err) {
reject(err);
} else {
resolve(stat);
}
});
});
}

https://github.com/koajs/examples/tree/master/stream-server-side-events

https://github.com/koajs/examples/tree/master/templates/

demo

https://stackoverflow.com/questions/51571054/koa2-how-to-write-to-response-stream


'use strict';
const koa = require('koa');
const fs = require('fs'); const app = new koa(); const readable = require('stream').Readable
const s = new readable; // response
app.use(ctx => {
if (ctx.request.url === '/stream') {
// stream data
s.push('STREAM: Hello, World!');
s.push(null); // indicates end of the stream
ctx.body = s;
} else if (ctx.request.url === '/file') {
// stream file
const src = fs.createReadStream('./big.file');
ctx.response.set("content-type", "txt/html");
ctx.body = src;
} else {
// normal KOA response
ctx.body = 'BODY: Hello, World!' ;
}
}); app.listen(8080);

OK


"use strict"; /**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-02-22
* @modified
*
* @description
* @augments
* @example
* @link
*
*/ const log = console.log; const Koa = require('koa');
const app = new Koa(); const fs = require('fs');
const path = require('path');
// const img = require('../poster.png'); // const readable = require('stream').Readable
// const s = new readable; const img = fs.createReadStream('./poster.png'); // logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
log(`${ctx.method} ${ctx.url} - ${rt}`);
}); // x-response-time
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
}); // response
app.use(async ctx => {
// node.js debugger
// log(`ctx`, ctx);
// log(`\nctx.request =\n`, JSON.stringify(ctx.request, null, 4));
// log(`\nctx.response \n`, JSON.stringify(ctx.response, null, 4));
// ctx.body = 'Hello World';
// ctx.body = img;
ctx.response.set("content-type", "image/png");
ctx.body = img;
// response image
}); app.listen(3000);

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


koa response image的更多相关文章

  1. koa简介

    资料来源:http://javascript.ruanyifeng.com/nodejs/koa.htmlhttp://koa.bootcss.com/ 以下内容为摘抄,纯属做笔记加深印象.勿喷. 使 ...

  2. 【转载】Express、Koa、Hapi框架对比

    中文翻译:http://ourjs.com/detail/5490db1c8a34fa320400000e 英文原文:https://www.airpair.com/node.js/posts/nod ...

  3. KoaHub平台基于Node.js开发的Koa的模板系统handlebars插件代码详情

    koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...

  4. 基于 Koa平台Node.js开发的KoaHub.js的模板引擎代码

    koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...

  5. KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架之koahub-handlebars

    koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...

  6. Koa 学习笔记

    开始 就像官网上说的,一切框架都从一个"Hello World"开始,首先我们新建一个 package.json,内容尽量简单: { "name": " ...

  7. Koa 框架教程

    Koa 框架教程   作者: 阮一峰 日期: 2017年8月 9日 Node 主要用在开发 Web 应用.这决定了使用 Node,往往离不开 Web 应用框架. Koa 就是一种简单好用的 Web 框 ...

  8. egg 官方文档之:框架扩展(Application、Context、Request、Response、Helper的访问方式及扩展)

    地址:https://eggjs.org/zh-cn/basics/extend.html Application app 对象指的是 Koa 的全局应用对象,全局只有一个,在应用启动时被创建. 访问 ...

  9. Koa 框架整理

    学习交流 Koa使用了ES6规范的generator和异步编程是一个更轻量级Web开发的框架,Koa 的先天优势在于 generator.由于是我个人的分享交流,所以Node基础.ES6标准.Web开 ...

随机推荐

  1. 目前 c++ primer学习总结笔记

    C++的开始 1 main的返回值:0为成功状态,非0为系统定义的错误类型 2 输入输出:计算结果为左侧运算对象,IO操作读写缓冲与程序中的动作无关 3 输入流istream对象:cin(标准输入): ...

  2. DDD领域驱动设计:领域事件

    1 前置阅读 在阅读本文章之前,你可以先阅读: DDD领域驱动设计是什么 DDD领域驱动设计:实体.值对象.聚合根 DDD领域驱动设计:仓储 MediatR一个优秀的.NET中介者框架 2 什么是领域 ...

  3. centos7+python3+selenium+chrome

    一.安装GUI图形化界面 (1)安装GUI图形化界面 yum groupinstall "GNOME Desktop" "Graphical Administration ...

  4. Jmeter(三十七) - 从入门到精通进阶篇 - 输出HTML格式的性能测试报告(详解教程)

    1.简介 相对于Loadrunner,Jmeter其实也是可以有测试报告产出的,虽然一般都不用(没有Loadrunner的报告那么强大是一方面),但是有小伙伴们私下问,那宏哥还是顺手写一下吧,今天我们 ...

  5. python 基础学习3 列表和元组 、字符串

    作为小白,坚持每日写学习记录,是督促坚持学习的动力, 今天主要是学习 列表和元组,列表是可以修改的,元组是不可变的.列表和元组的索引都是从0开始 列表可以修改, 可以对列表进行赋值,修改移除等各种方法 ...

  6. H5 的直播协议和视频监控方案

    H5 的直播协议和视频监控方案 一.流媒体主要实现方式 二.流媒体技术 2.1 流媒体 2.2 直播 2.3 流协议 2.3.1 HLS 协议 2.3.2 RTMP 协议 2.3.3 RTSP 协议 ...

  7. Phoenix表和索引分区优化方法

    Phoenix表和索引分区,基本优化方法 优化方法 1. SALT_BUCKETS RowKey SALT_BUCKETS 分区 2. Pre-split RowKey分区 3. 分列族 4. 使用压 ...

  8. Spark高级数据分析——纽约出租车轨迹的空间和时间数据分析

    Spark高级数据分析--纽约出租车轨迹的空间和时间数据分析 一.地理空间分析: 二.pom.xml 原文地址:https://www.jianshu.com/p/eb6f3e0c09b5 作者:II ...

  9. chm打开看不到内容时好时坏

    右击chm文件,属性====>解除锁定 再打开 ok

  10. zabbix监控设备结果异常问题

    1.现象描述 paloalto防火墙升级后发现zabbix监控其CPU.风扇等硬件信息和端口流量数据错误. 2.现象分析 由于zabbix并没有做过任何调整,防火墙也只是升级.根据现象并不好排查.但是 ...