koa

koa2 长连接 koa-websocket

浪尽此生 提交于 2019-12-02 22:24:40
引入npm依赖 npm i -S koa npm i -S koa-route npm i -S koa-websocket    1.app.js const port = 9501; const Koa = require('koa') // 路由 const route = require('koa-route') // koa封装的websocket这是官网(很简单有时间去看一下https://www.npmjs.com/package/koa-websocket) const websockify = require('koa-websocket') const app = websockify(new Koa()); app.ws.use(function (ctx, next) { ctx.websocket.send("连接成功"); return next(ctx) }) app.ws.use(route.all('/', function (ctx) { /**接收消息*/ ctx.websocket.on('message', function (message) { console.log(message); // 返回给前端的数据 let data = JSON.stringify({ id: Math.ceil(Math.random()*1000),

Koa - 中间件

匿名 (未验证) 提交于 2019-12-02 21:53:52
前言 Koa 应用程序是一个包含一组中间件函数的对象,它是按照类似堆栈的方式组织和执行的。 next() 以上两句话,是我在官方文档中找到其对 Koa 中间件的描述。 在Koa中,中间件是一个很有意思的设计,它处于request和response中间,被用来实现某种功能。像上篇文章所使用的 koa-router 、koa-bodyparser 等都是中间件。 可能有些人喜欢把中间件理解为插件,但我觉得它们两者并不是同一种概念的东西。插件像是一个独立的工具,而中间件更像是流水线,将加工好的材料继续传递下一个流水线。所以中间件给我的感觉更灵活,可以像零件一样自由组合。 单看中间件有堆栈执行顺序的特点,两者就出现质的区别。 中间件的概念 这张图是 Koa 中间件执行顺序的图示,被称为“洋葱模型”。 中间件按照栈结构的方式来执行,有“先进后出“的特点。 一段简单的代码来理解上图: app.use(async (ctx, next)= console.log('--> 1') next() console.log('<-- 1') }) app.use(async (ctx, next)=>{ console.log('--> 2') //这里有一段异步操作 await new Promise((resolve)=>{ .... }) await next() console.log('<-

Koa - 初体验(写个接口)

匿名 (未验证) 提交于 2019-12-02 21:53:52
前言    不会 n ode.js 的前端不是一个好前端!   这几年 node.js 确实是越来越火了,好多公司对 node.js 都开始有要求。虽说前端不一定要会后端,但想要成为一个优秀的前端, node.js 是必经之路。   我对于 node.js 的第一印象,认为它是一门后端语言,只是前端学习起来成本会更低更好上手。慢慢经过了解后,使用 node.js 写接口对于前端来说很方便,但不仅限于写接口。在一些大公司里, node.js 并不是开发接口的首选目标,而是作为中间层来使用。我们都知道分工合作,让专业的人做更专业的事,工作效率会大大提高。 node.js 作为中间层的存在,可以让后端更专注于写接口和管理数据。   试想一下,现在由于业务逻辑改变,后端需要对数据接口进行更改,是否会花费时间?如果你会 node.js ,那么你就可以通过 node.js 来做数据聚合,从几个接口中拼接数据供前端使用,而不需要为数据结构和数据内容烦恼,并且你不用担心效率,因为 node.js天生异步。    包括我们常用的一些脚手架工具也是基于 node.js 环境搭建,你甚至还可以使用 node.js 来做数据挖掘,也就是我们所说的爬虫, node.js 的应用层面还有很多。以上都是我了解到的一些信息。   目前 node.js 比较主流框架分为express、koa、egg。

How can I split my koa routes into separate files?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 14:42:38
I'm trying to figure out how to split my routes into separate files. I have this so far, but it doesn't work. I just get Not found when I try to access http://localhost:3001/api/things //server.js var koa = require('koa'); var app = koa(); var router = require('koa-router'); app.use(router(app)); require('./routes')(app); // routes.js module.exports = function *(app){ app.use('/api/things', require('./api/things')); }; // api/things/index.js var Router = require('koa-router'); var router = new Router({ prefix: '/api/things' }); router.get('/', function *(){ this.body = [{ name: 'Foo'}, { name:

koa学习之路二

烂漫一生 提交于 2019-12-02 11:47:01
一、Koa 路由 路由(Routing)是由一个 URI(或者叫路径)和一个特定的 HTTP 方法(GET、POST 等)组成的,涉及到应用如何响应客户端对某个网站节点的访问。 通俗的讲:路由就是根据不同的 URL 地址,加载不同的页面实现不同的功能。 Koa 中的路由和 Express 有所不同,在 Express 中直接引入 Express 就可以配置路由,但是在 Koa 中我们需要安装对应的 koa-router 路由模块来实现。 npm install koa-router --save 我们接着上一节的项目目录来编写,将我们的 app.js 文件改为如下: //引入 koa模块 var Koa = require('koa'); var Router = require('koa-router'); //实例化 var app = new Koa(); var router = new Router(); //配置路由 router.get('/', async (ctx) => { // ctx 上下文 context ,包含了request 和response等信息 // 返回数据 相当于:原生里面的res.writeHead() res.end() ctx.body = '首页'; }); router.get('/news', async (ctx) => {

koa学习之路一

北城余情 提交于 2019-12-02 11:31:16
首先我们先创建一个我们要开发的项目的目录中,然后运行 npm init ,为我们的项目起个名字,然后一路回车,这样就会在我们的项目目录中创建一个最基本的 package.json 的文件,接下来安装我们要使用的 koa 模块 npm install koa --save ,这样我们就引入了 koa 模块,接下来就和 Express 模块是一样的操作流程,我们在项目目录中创建一个 app.js,在 app.js 中写我们的程序就可以了。 最终创建的项目目录如下: 在 app.js 中写一个简单的案例: var koa = require("koa"); var app = new koa(); // Express 写法 // app.use(function (req, res) { // res.send("hello word") // }); app.use(async (ctx) => { ctx.body = "hello word" }); app.listen(3000); 在上面的代码中,我们首先引入 koa 模块,然后创建一个 koa 实例,接下来和 Express 一样的是创建一个中间价,然后写一个异步函数来向前端页面返回数据。 Express 的写法在这里就不做过多解释了,我们来看一下 koa 写法,我们通过 async ()=>{} 的异步方法与前端建立连接

How to run simple app with koa2?

半腔热情 提交于 2019-12-02 01:39:11
Problem I am trying to run simple http server using koa2 , but have problems running it. It uses es6 that is expected to work in future node.js versions and I was wondering how can I run it with node v6.1.0 ? Code import Koa from 'koa'; const app = new Koa(); // Setup handler. app.use(async ctx => { ctx.body = "Hello World!"; }); // Start server. app.listen(3000); Output $ node --version v6.1.0 $ node --harmony index.js C:\Users\gevor\WebstormProjects\untitled1\index.js:1 (function (exports, require, module, __filename, __dirname) { import Koa from 'koa'; ^^^^^^ SyntaxError: Unexpected token

Vue使用vue-recoure + http-proxy-middleware + vuex配合promise实现基本的跨域请求封装

放肆的年华 提交于 2019-12-01 18:34:19
使用 vue init webpack 你的项目名称 初始化一个vue的项目 安装依赖 npm install vue-resource http-proxy-middleware vuex koa 在项目的main.js中引入并注册下载的依赖 在main.js中引入 vue-resource 并注册到vue实例中 import VueResource from 'vue-resource' //用来请求接口 Vue.use(VueResource) //开启后请求就会以application/x-www-form-urlencoded作为MIME type 就像普通的html表单一样,form形式传给后台,而非JSON Vue.http.options.emulateJSON = true; 在main.js文件同级创建一个store的文件夹用来存放vuex的文件 在store文件夹中创建一个index.js的文件(vue在查找时会自动查找这个文件),在文件中创建store实例并导出 import Vue from 'vue' import VueX from 'vuex' import actions from './actions' //这里为了可扩展性,所以单独创建一个文件用来存放请求方法 Vue.use(VueX); //请求之甬道actions所以只导入了actions

Can not set Header in KOA When using callback

谁都会走 提交于 2019-12-01 08:28:25
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:11) at Object.module.exports.set (G:\NAP\node_modules\koa\lib\response.js:396:16) at Object.length (G: