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. 一个基于protocol buffer的RPC实现

    Protocol Buffer仅仅是提供了一套序列化和反序列化结构数据的机制,本身不具有RPC功能,但是可以基于其实现一套RPC框架. Services protocol buffer的Service ...

  2. LOJ10064黑暗城堡

    题目描述你知道黑暗城堡有 N 个房间,M 条可以制造的双向通道,以及每条通道的长度. 城堡是树形的并且满足下面的条件: 设 Di​ 为如果所有的通道都被修建,第 i 号房间与第 1 号房间的最短路径长 ...

  3. LOJ10163 Amount of Degrees

    题目描述 求给定区间 [X,Y] 中满足下列条件的整数个数:这个数恰好等于 KK 个互不相等的 BB 的整数次幂之和.例如,设 X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意 输入格 ...

  4. 通过脚本本地下载Jar包

    通过脚本本地下载Jar包 1.脚本 2.pom.xml 1.脚本 download.bat # !/bin/bash mvn -f pom.xml dependency:copy-dependenci ...

  5. SQL系列总结——基础篇(二)

           表达式:是符号与运算符的组合,一个常量,变量.列或标量函数就是一个简单的表达式,复杂的表达式由两个或更多的简单表达式通过运算符连接起来组成.运算符同样的遵循优先级的原则.作用,让SQL的 ...

  6. Kafka调试入门(一)

    很多人对kafka消息队列应该不陌生,使用起来也比较方便.对kafka最常见的操作一般有如下几种: 启动kafka集群 创建一个名称为xxx的主题(topic) 查看已经创建好的主题 向xxx这个主题 ...

  7. redis-避免生产环境使用keys命令

    redis作为内存数据库, 有着很高的性能, Redis能读的速度是110000次/s, 写的速度是81000次/s; 除了进行持久化操作时, redis采用的是单线程架构, 所以如果我们在开发中不恰 ...

  8. Educational Codeforces Round 17

    Educational Codeforces Round 17 A. k-th divisor 水题,把所有因子找出来排序然后找第\(k\)大 view code //#pragma GCC opti ...

  9. Educational Codeforces Round 91 (Rated for Div. 2) A. Three Indices

    题目链接:https://codeforces.com/contest/1380/problem/A 题意 给出一个大小为 $n$ 的排列,找出是否有三个元素满足 $p_i < p_j\ and ...

  10. 【uva 1612】Guess(算法效率,2种想法)

    题意:已知 N 位选手的3题的预期得分,得分要不全拿,要不为0.且知道最后的实际名次,而且得分相同的选手,ID小的排在前面.问这样的名次可能吗.若可能,输出最后一名的最高可能得分.(N≤16384) ...