koa

Can not set Header in KOA When using callback

青春壹個敷衍的年華 提交于 2019-12-01 06:01:15
问题 Recently I worked on a new project that used javascript callbacks. And I was using koa framework. But when I called this route : function * getCubes(next) { var that = this; _OLAPSchemaProvider.LoadCubesJSon(function(result) { that.body = JSON.stringify(result.toString()); }); } I get this error : _http_outgoing.js:331 throw new Error('Can\'t set headers after they are sent.'); ^ Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:331

Google Cloud Functions, Node JS 8.9.x (LTS) and KOA library

僤鯓⒐⒋嵵緔 提交于 2019-12-01 05:35:23
How can I use Koa library, the express replacement, in Cloud Functions? I know KOA use all great ES2017 and make more use of Async use of JavaScript. or it might not be needed at all working with Cloud Functions because the Firebase system won't send multiple calls to the same Cloud Function until it ends the previous one? it unclear to me. it know demands Node 8.x and I know the NodeJs 8.9.x, has now LTS. Reading from cloud functions doc : Base Image Cloud Functions uses a Debian-based execution environment and includes contents of the gcr.io/google-appengine/nodejs Docker image, with the

初识KOA(上)

有些话、适合烂在心里 提交于 2019-12-01 05:07:59
新一代node框架入门,前置知识:node基础,数据库基础,了解Koa怎么搭建服务器的,不适合通读, 推荐跟文章实际操作 (手把手教学) 如果有 知识点 未知请看: ejs koa文档 前端er,你真的会用 async 吗? async/await 应知应会 如何避开 async/await 地狱 之前对JS异步,这一块有点生疏,多看点博客 简介 Koa 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小、更富有表现力、更健壮的基石。 通过利用 async 函数,Koa 帮你丢弃回调函数,并有力地增强错误处理。 Koa 并没有捆绑任何中间件, 而是提供了一套优雅的方法,帮助您快速而愉快地编写服务端应用程序。 1.新建文件夹 初始化package.json,终端输入 npm init --yes 2.下载KOA 终端输入 cnpm install koa 当前文件夹下会出来node_moudules,模块依赖包 3.安装nodemon 它会监测项目中的所有文件,一旦发现文件有改动,Nodemon 会自动重启应用 cnpm install nodemon -D -g 安装全局 4.在package.json中配置nodemon 123 "scripts": { "start": "nodemon app.js"

How can I use ES2016 (ES7) async/await in my acceptance tests for a Koa.js app?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 22:51:17
I am in the process of writing my first Koa.js app, and having recently been introduced to the ES2016 (aka ES7) features of async / await , I wanted to make use of these. I found that my Google skills were not up to the task, and the few snippets of code I could find were either for the standard Koa (using generators) or otherwise not-as-bleeding-edge-as-ES7. See my answer below for how I got my tests running. I'm still a beginner, so it's likely that a lot of this can be optimised considerably, but here's what worked for me. I'll basically just dump my files here, they should be fairly

koa2初步使用

ε祈祈猫儿з 提交于 2019-11-30 21:17:48
看了 廖雪峰 的 node教程,koa教程没有最终完整代码,自己归纳记录下 需要用到的三个koa模块 : npm init npm install koa -S npm install koa-router -S npm install koa-bodyparser -S 目录结构: koa/ | +- controllers/ | | | +- login.js <-- 处理login相关URL | | | +- users.js <-- 处理用户管理相关URL | +- app.js <-- 使用koa的js | +- controller.js <-- 路由js | +- package.json <-- 项目描述文件 | +- node_modules/ <-- npm安装的所有依赖包 //api.js const Koa = require('koa'); const app = new Koa(); //由于middleware的顺序很重要,这个koa-bodyparser必须在router之前被注册到app对象上 const bodyParser = require('koa-bodyparser'); app.use(bodyParser()); const controller = require('./controller'); app.use

node+koa2+mysql搭建博客后台

夙愿已清 提交于 2019-11-30 15:42:25
本文将详细讲解使用node+koa2+mysql搭建博客后台的全过程。 开发环境 node 8.3.0及以上 npm 5.3.0及以上 mysql 5.7.21 具体的环境配置可查看我的 上一篇文章 准备工作 npm下载pm2(进程守护),并设置全局变量 创建博客需要的数据库与表 开启mysql并创建数据库test: create database test; 切换到数据库test use tests; ,输入命令创建以下数据表: //系统管理员表 t_user CREATE TABLE `t_user` ( `uid` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL COMMENT '姓名' , `password` varchar(50) NOT NULL COMMENT '密码' , `create_time` datetime NOT NULL COMMENT '注册时间' , `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间' , `is_delete` tinyint(1) DEFAULT '0' , PRIMARY KEY (`uid`), UNIQUE

koa和exprsss区别

浪子不回头ぞ 提交于 2019-11-30 15:05:31
koa和exprsss区别 koa没有内置中间件 express有几个内置的中间件,如express.static()//加载静态资源 koa不再有req,res请求,它是封装在context里面 ctx express是有req请求数据,res相应内容 koa自己封装了路由规则 安装 全局安装:koa-generator框架 npm install koa-generator -g 新建一个文件夹 :myapp cd myapp koa -h :查看koa框架内容 npm -e myapp 环境安装 :自动生成文件夹 npm install : 安装文件结构目录 来源: https://www.cnblogs.com/clover-liu/p/11600248.html

node中间件KOA函数

感情迁移 提交于 2019-11-28 19:55:21
const Koa = require('koa'); const app = new Koa() //应用程序对象 中间件 // 发送HTTP KOA 接手HTTP //中间件(其实就是 函数) function test(){ console.log("seven month"); } //当请求发送过来的时候,将函数(中间件)注册到程序上 //前端发送一个http请求 来触发中间件 //koa 中 只会执行第一个中间件 app.use(async(ctx, next)=>{ //ctx 上下文 // next 下一个中间函数 console.log("7") const a = await next() //调用下一个中间件 // await 可以将promise 中对象 值 直接获取(不通过then) // await 对表达式求值 //await 阻塞线程(异步) (将异步变为同步) console.log(a) // a.then((res)=>{ // console.log(res) // }) console.log("8") }) // 洋葱模型(如果有await, 则需要在每个中间件函数前面加async next前面加上 await, ha) app.use(async(ctx, next) =>{ console.log("9") const res =

十分钟带你看完 KOA 源码v

浪子不回头ぞ 提交于 2019-11-28 19:52:24
前段时间看了 koa 源码,得益于 koa 良好抽象,不仅提供了简洁的 api ,同时也使得源码相当的简洁和优雅。今天花点时间画了一张 koa 源码的结构图来分析其源码,在总结的同时,希望能够帮到相关的同学。 注:源码是基于 2.x 版本,源码结构与 1.x 完全一致,代码更加简洁直观一点。 基础知识 任何用过 node 的人对下面的代码都不会陌生,如下: const http = require(‘http’); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader(‘Content-Type’, ‘text/plain’); res.end(‘Hello World\n’); }); server.listen(3000); 上面的代码很简单,http 的 createServer 方法创建了一个 http.Server 的实例,http.Sever 继承于 EventEmitter 类。我们传入的函数,其实是添加到了 server 的 request 事件中,相当于一种快捷方式。所以你也完全可以不传入任何参数,然后手动去监听 request 事件。代码的最后将 server 绑定到 3000 端口,开始监听所有来自 3000 端口的请求。 然后,我们看一下

How to run Generator Functions in Parallel?

泄露秘密 提交于 2019-11-28 10:37:18
问题 Assuming I have a Koa web server with an endpoint like this: const perform = require(...); // some generator function exports.endpoint = function* () { var results = yield getResults(); // Respond the results this.body = results; } exports.getResults = function* () { var actions = [...]; var results = []; for (var action of actions) { var result = yield perform(action); results.push(results); } return results; } Now the client will get the respond after ALL the actions are performed obviously