koa

Koa.js 设计模式-学习笔记

我是研究僧i 提交于 2019-12-15 15:13:02
前言 之前写过一本 《Koa2进阶学习笔记》 作为Koa的入门教程。很多知识点都是一笔带过,没有深入的讲解。这一本书是通过Koa.js的常用中间件实现原理,举一反三来讲解一些Node.js在Web开发过程中的原理和设计模式。 Koa.js 是一个极其精简的Web框架,只提供一下两种功能: HTTP服务 处理HTTP请求request 处理HTTP响应response 中间件容器 中间件的加载 中间件的执行 剩下的其他Web服务所需的能力,就根据开发者的需求去自定义开发,留下了很大的灵活空间,提高了Web服务的开发成本。在我的理解中,Koa.js的灵活度带来的开发成本有以下两种: 框架的设计 中间件的选择 框架的设计,这一因素比较复杂,后续会新开一本书讲解。本书主要是解析常用的Koa.js中间件,抽象出相关中间件的功能原理和实现方式,用demo让读者理解原理,减少对官方源码的依赖,尽量达到“授人予渔”。 目录 Koa.js 原理 1.1 学习准备 1.2 Promise 使用 1.3 async/await 使用 1.4 Node.js原生http模块 1.5 中间件引擎 1.6 普通中间件式HTTP服务实现 1.7 最简Koa.js实现 Koa.js 的AOP设计 2.1 AOP面向切面编程 2.2 洋葱模型切面 2.3 HTTP切面流程 Koa.js 中间件 3.1 中间件分类

How Do I Test nested ES6 Generators using Mocha?

假装没事ソ 提交于 2019-12-14 03:55:43
问题 I'm trying to use co-mocha to test some nested generators functionality in my koa app. The class works just fine at runtime, but when I attempt to test the functionality, I cannot get the nested generator to run in my test. Class being tested: import Promise from 'bluebird' class FooService { _doAsync(){ return new Promise((resolve) => { setTimeout(() => { resolve({ foo: 'FOO' }) }, 500) }) } create(){ console.log('This never gets logged') let self = this return function*(){ console.log(`This

Koa.js request with promises is hanging

泪湿孤枕 提交于 2019-12-13 06:52:59
问题 So I'm messing around with Koa.js and generators, just threw together a simple site for demo purposes. I'm using sqlite with node-sqlite3 and Q for promises. Here's my db code: module.exports.getLogs = function(){ var deferred = Q.defer(); var results = []; db.serialize(function(){ db.each("SELECT ipAddress, action, details, timestamp FROM logs", function(err, row) { results.push({ ipAddress: row.ipAddress, action: row.action, details: row.action, timestamp: new Date(row.timestamp) }); },

Koa-router and POST

大憨熊 提交于 2019-12-13 01:01:08
问题 I'm trying to handle POST request within my koa-router. Unfortunately, every time I try to get data send using my form, I get nothing. I've tried koa-bodyparser, no luck there. I'm using Jade as template engine. router.js: var jade = require('jade'); var router = require('koa-router')(); var bodyParser = require('koa-bodyparser'); exports.enableRouting = function(app){ app.use(bodyParser()) router.get('/game/questions', function *(next){ this.status = 200; this.body = jade.renderFile('game

webpack-dev-server with backend api

半腔热情 提交于 2019-12-12 10:47:29
问题 I am using webpack-dev-server for an angularjs app, I start it from a task in package.json like this: "scripts": { "start-api": "node api/server.js", "dev": "webpack-dev-server --env dev --history-api-fallback --inline --progress --port 5000", "start": "npm run dev" }, I have a backend api server that uses koa and is running on the same port: const koa = require('koa'); app.listen(5000); module.exports.app; When the koa server is started, it intercepts all requests and I cannot browse to the

“Generator already running” message on any error

纵然是瞬间 提交于 2019-12-12 04:55:02
问题 I have a very simple generator function, like this exports.whatever = function *(next) { this.body = 'wow'; z }; Now, there obviously is a syntax error here ('z' on the third line), but with koa's generators, the error I get in the console is basically useless for debugging; I do not get the line of the error, the file, or even the type of error. All I get is: Error: Generator is already running at GeneratorFunctionPrototype.next (native) at onFulfilled (/Users/johndoe/Documents/nodejs

What is the replacement of express's res.send({ user: 'xyz' }); or res.end() in koa?

不打扰是莪最后的温柔 提交于 2019-12-12 03:33:49
问题 I want to send the response with an error in my koa app when there is no sessionId .I explored But didn't get anything helpful for me to do the same. I also used return ctx.throw(401); for unauthorized but it is not good, ctx.throw(401); just sending the "unauthorized", I want to add some specific information and after adding just send the response to the client. can anyone suggest me what to do the same? My code is: index.validateKey = async (ctx, next) => { await new Promise((resolve,

Koa-compress is not working

无人久伴 提交于 2019-12-12 03:27:17
问题 This is my code for embedding koa-compress middleware app.use(compress({ filter: function (content_type) { return /text/i.test(content_type) }, threshold: 1, flush: require('zlib').Z_SYNC_FLUSH })) And following is my response sending code ctx.body = 'Hello world' ctx.compress = true ctx.set('Content-Type', 'text/plain') ctx.set('content-encoding', 'gzip') When I hit the URL through CURL I get a simple plain text saying "Hello World" but I believe I should have I got a compressed string

How to perform initial setup in koa with rethinkdb

痞子三分冷 提交于 2019-12-11 23:07:27
问题 Im using rethinkdbdash with koa and trying to figure out how to perform a set up of the database when the app first starts. Im trying to accomplish something similar to Segphalt's cats of instagram tutorial but using koa. var conn; r.connect(config.database).then(function(c) { conn = c; return r.dbCreate(config.database.db).run(conn); }) .then(function() { return r.tableCreate("instacat").run(conn); }) .then(function() { return q.all([ r.table("instacat").indexCreate("time").run(conn), r

Sequelize create not a function?

余生长醉 提交于 2019-12-11 21:25:50
问题 I am trying to use model.create and it's saying that model.create is not a function. I googled around and couldn't seem to find any rmesolutions. Please note that I'm using es6 imports / exports in node via babel. model.js 'use strict'; export default (sequelize, DataTypes) => { let attachments = sequelize.define('attachments', { type: DataTypes.STRING, category: DataTypes.STRING, value: DataTypes.STRING, }, {}); attachments.associate = (models) => { }; return attachments; }; controller