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. EF Code First 无法加载指定的元数据资源

    是由属于一般出现这个错误是由于App.config里面配置错误,DB First 是不一样的. 配置文件不止一个地方··多查查其他项目有没有.

  2. UserControl和CustomControl两者区别

    UserControl 将多个WPF控件(例如:TextBox,TextBlock,Button)进行组合成一个可复用的控件组: 由XAML和Code Behind代码组成: 不支持样式/模板重写: ...

  3. 小麦苗数据库巡检脚本,支持Oracle、MySQL、SQL Server和PG等数据库

    目录 一.巡检脚本简介 二.巡检脚本特点 三.巡检结果展示 1.Oracle数据库 2.MySQL数据库 3.SQL Server数据库 4.PG数据库 5.OS信息 四.脚本运行方式 1.Oracl ...

  4. Node.js躬行记(6)——自制短链系统

    短链顾名思义是一种很短的地址,应用广泛,例如页面中有一张二维码图片,包含的是一个原始地址(如下所示),如果二维码中的链接需要修改,那么就得发代码替换掉. 原始地址:https://github.com ...

  5. XCTF-黑客精神

    杂言 前段时间键盘坏了,电脑硬盘也坏了,买东西装系统再偷个懒放了一周左右假.期间学习巩固了一下安卓开发的知识.用了固态才知道什么叫纵享丝滑,当初就不该省这个钱. 前期工作 查壳,无.运行,点击按钮就跳 ...

  6. HBase,以及GeoMesa设计基于HBase的设计分析,从数据模型到典型查询场景,最后进行RowKey设计

    GeoMesa设计基于HBase的设计分析,从数据模型到典型查询场景,最后进行RowKey设计 一.HBase 基本概念 理解KeyValue KeyValue多版本 列定义(1) 列定义(2) Co ...

  7. navicat连接阿里云mysql数据库服务器遇到的1130等相关问题

    最近搭建自己的博客网站的时候使用了阿里云的云服务器ESC,搭建了一个mysql数据库,在windows下使用navicat连接mysql的时候,总是连接不上,出现了1130的问题等等错误信息. 下面将 ...

  8. Memcached 缓存系统简介

    memcached官网:http://memcached.org/ What is Memcached? Memcached是一个自由开源的,高性能,高并发,分布式内存对象缓存系统. Memcache ...

  9. php-浅谈php底层机制

    php-浅谈php底层机制 1. PHP的设计理念及特点 多进程模型:由于PHP是多进程模型,不同请求间互不干涉,这样保证了一个请求挂掉不会对全盘服务造成影响,当然,随着时代发展,PHP也早已支持多线 ...

  10. linux虚拟摄像头vivid配置

    总述    最近在看摄像头驱动,需要配置虚拟摄像头的驱动,但是教程里面是linux2.6内核的,实际电脑的是Ubuntu16,内核是linux4.15版本,从2.6到4.15内核好多文件发生了变化,所 ...