koa response image
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的更多相关文章
- koa简介
资料来源:http://javascript.ruanyifeng.com/nodejs/koa.htmlhttp://koa.bootcss.com/ 以下内容为摘抄,纯属做笔记加深印象.勿喷. 使 ...
- 【转载】Express、Koa、Hapi框架对比
中文翻译:http://ourjs.com/detail/5490db1c8a34fa320400000e 英文原文:https://www.airpair.com/node.js/posts/nod ...
- KoaHub平台基于Node.js开发的Koa的模板系统handlebars插件代码详情
koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...
- 基于 Koa平台Node.js开发的KoaHub.js的模板引擎代码
koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...
- KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架之koahub-handlebars
koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...
- Koa 学习笔记
开始 就像官网上说的,一切框架都从一个"Hello World"开始,首先我们新建一个 package.json,内容尽量简单: { "name": " ...
- Koa 框架教程
Koa 框架教程 作者: 阮一峰 日期: 2017年8月 9日 Node 主要用在开发 Web 应用.这决定了使用 Node,往往离不开 Web 应用框架. Koa 就是一种简单好用的 Web 框 ...
- egg 官方文档之:框架扩展(Application、Context、Request、Response、Helper的访问方式及扩展)
地址:https://eggjs.org/zh-cn/basics/extend.html Application app 对象指的是 Koa 的全局应用对象,全局只有一个,在应用启动时被创建. 访问 ...
- Koa 框架整理
学习交流 Koa使用了ES6规范的generator和异步编程是一个更轻量级Web开发的框架,Koa 的先天优势在于 generator.由于是我个人的分享交流,所以Node基础.ES6标准.Web开 ...
随机推荐
- 日记 + sb错误
置顶消息cpdd 1.29 完了,文化课没了 我是废物 1.28 更新了自己的副标题 前副标题:Future never has to do with past time,but present ti ...
- 解决 ThinkPHP5 RCE 在PHP7下,不能使用包含的问题
今天朋友遇到一个ThinkPHP5 _method 的RCE漏洞,环境是:tp5014开启debug,linux,PHP7,日志,Session都写不进去,没办法包含的情况. 思路就是使用反序列化,回 ...
- scala 两个map合并,key相同时value相加/相减都可
scala 两个map合并,key相同时value相加 1.map自带的合并操作 2.map函数 2.1示例 2.2合并两个map 3.用foldLeft 3.1 语法 3.2 合并两个map 1.m ...
- Scala安装后,在IDEA中配置
IDEA中配置Scala 一.设置Module 二.添加Scala的SDK 三.寻找本地scala安装路径 四.测试是否可以新建有Scala Class 五.踩坑填坑记录 5.1:Error:(4, ...
- 文本处理三剑客简介(grep、awk、sed)
本章内容: 命令 描述 awk 支持所有的正则表达式 sed 默认不支持扩展表达式,加-r 选项开启 ERE,如果不加-r 使用花括号要加转义符\{\} grep 默认不支持扩展表达式,加-E 选项开 ...
- 2. Linux常用系统工作命令
1.echo:在终端输出字符串或变量提取后的值.echo [字符串 | $变量] 举例:[root@Centos~]# echo $SHELL /bin/bash 2.date:显示及设置系统的时间或 ...
- Java二维数组转成稀疏sparsearray数组
稀疏数组 基本介绍 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 记录数组一共有几行几列,有多少个不同的值 把具有不同值的元素的行列及值记 ...
- Educational Codeforces Round 97 (Rated for Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1437 A. Marketing Scheme 题解 令 \(l = \frac{a}{2}\),那么如果 \(r < ...
- Codeforces Round #675 (Div. 2)【ABCD】
比赛链接:https://codeforces.com/contest/1422 A. Fence 题意 给出三条边 $a,b,c$,构造第四条边使得四者可以围成一个四边形. 题解 $d = max( ...
- poj 2398Toy Storage
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3146 Accepted: 1798 Descr ...