koa

Koa源码浅析

安稳与你 提交于 2019-12-31 17:26:11
Koa源码十分精简,只有不到2k行的代码,总共由4个模块文件组成,非常适合我们来学习。 参考代码: learn-koa2 我们先来看段原生Node实现Server服务器的代码: const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200); res.end('hello world'); }); server.listen(3000, () => { console.log('server start at 3000'); }); 非常简单的几行代码,就实现了一个服务器Server。 createServer 方法接收的 callback 回调函数,可以对每次请求的 req res 对象进行各种操作,最后返回结果。不过弊端也很明显, callback 函数非常容易随着业务逻辑的复杂也变得臃肿,即使把 callback 函数拆分成各个小函数,也会在繁杂的异步回调中渐渐失去对整个流程的把控。 另外,Node原生提供的一些API,有时也会让开发者疑惑: res.statusCode = 200; res.writeHead(200); 修改 res 的属性或者调用 res 的方法都可以改变 http 状态码,这在多人协作的项目中,很容易产生不同的代码风格。

koa项目好用的插件

喜夏-厌秋 提交于 2019-12-31 12:36:49
1.koa-bodyparser koa不能直接获取请求体里的body,需要安装一个中间件(koa-bodyparser) 详细内容见我的另一篇博客: https://www.cnblogs.com/kaiqinzhang/p/12091500.html 2.错误处理并返回json格式插件: koa-json-error,使用方法很简单: 在入口文件引入: const error = require('koa-json-error'); app.use(error()) 即可处理4**,5**等错误,并返回详细错误信息。 3.效验参数:koa-parameter 在入口文件引入: const Koa = require('koa'); const bodyparser = require('koa-bodyparser'); const parameter = require('koa-parameter'); const app = new Koa(); const error = require('koa-json-error'); const routing = require('./routes'); app.use(error()); app.use(bodyparser()); app.use(parameter(app)); routing(app); app

nodejs+koa2 实现一个get请求

孤者浪人 提交于 2019-12-30 21:16:58
html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> </html> <script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script> <script> //步骤一:创建异步对象 let ajax = new XMLHttpRequest(); //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端 ajax.open('get', 'http://127.0.0.1:3000');//设置我们的请求头信息,post方法才写请求头 //post才写的请求头 ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //步骤三:发送请求 ajax.send(); //步骤四:注册事件 onreadystatechange 状态改变就会调用 ajax.onreadystatechange = function () { if (ajax.readyState == 4

路由参数

…衆ロ難τιáo~ 提交于 2019-12-27 01:46:06
ctx.params的使用: 当我把对应参数传过去,ctx.params就会得到相应的值,必传才行。 const Koa = require ( "koa" ) ; const Router = require ( "koa-router" ) ; let server = new Koa ( ) ; let router = new Router ( ) ; server . listen ( 8000 ) ; router . get ( '/user/:id/:name/:age' , async ctx => { ctx . body = ctx . params ; } ) ; server . use ( router . routes ( ) ) 命名路由: 用于便捷的生成url、跳转 匿名路由: router.get(’/’, async ctx=>{}) 命名路由:router.get(‘name’, ‘/’, async ctx=>{} const Koa = require ( "koa" ) ; const Router = require ( "koa-router" ) let server = new Koa ( ) ; let router = new Router ( ) ; server . listen ( 8000 ) ; router .

10 款 Node.js 框架,可用于你的下一个项目

守給你的承諾、 提交于 2019-12-25 10:56:47
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Node.js 是一个开源的跨平台运行时环境,用于开发服务器端和网络应用程序,它基于 Google Chrome V8 JavaScript 引擎构建,你可以使用 Node.js 中的 JavaScript 来实现用 Ruby 或 PHP 所做的一切东西。 本文整理了 10 款 Node.js 框架,根据应用程序功能将框架分为三类:API,全栈和类 Sinatra 的框架。文中的数据统计于各项目的 GitHub 主页(时间为 2016.12.20)。以后可能会有变动。 API 以下框架由 API 驱动,是快速部署 Node.js API 服务器的理想选择。 1. Actionhero 这是一个适用于 Node.js 的快速、轻量级和多重传输的 API 服务器,非常适合创建一个易于使用的工具包来制作可重用和可扩展的 API。集成了集群性能和延迟任务的功能。 Actionhero 可提供上层的支持(例如通过 JSON 集成),同时也可以在底层工作(例如与 TCP 协议通信)。它具有集群准备、内置支持后台任务和零停机部署的功能,并且它能像 CDN 一样分发而不需要运行 Apache 或 Nginx。 GitHub stars:1,511 GitHub contributors:68 2. Loopback 这是一个由

What's the recommend code for koa-ejs using koa2?

﹥>﹥吖頭↗ 提交于 2019-12-25 07:41:42
问题 I planed use ejs in koa2, my codes were like as blelow: render(app, { root: path.join(__dirname, 'views-ejs'), layout: 'layout', viewExt: 'ejs', cache: false, debug: true }); app.use(function *() { yield this.render('index',{ title: 'koa2 title', viewClass: 'landing', targetAuthLevel:'', authorizationLevel:'6', ngController: 'landingController' }); }); But, I get the below warning, would you tell me what's codes are recommended? please. koa deprecated Support for generators will been removed

Automatically logging in a user with KoaJS and PassportJS

丶灬走出姿态 提交于 2019-12-25 03:34:52
问题 I'm trying to automatically log in a user with PassportJS. This is my current code: myRouter.get('/signin', function* (next) { user = {...}; var res = this.res; // needed for the function below this.req.login(user, function(err) { if (err) console.log('error logging in user - '+err); return res.redirect('/'); // <--- line 439 }); }); But when I run it, I get the error: error logging in user - TypeError: undefined is not a function TypeError: undefined is not a function at /srv/www/domain.com

Koa学习笔记 了解Koa和创建路由实例

拟墨画扇 提交于 2019-12-24 15:11:25
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 通过对 14nodejs(7天) 的学习,已经对 Node.js 略有了解 终于尝试着逐渐接触自己一直想做而未达成的目标 微信公众订阅号开发 当然,以自己目前的水平,依然要依托教程的帮助,这里要学习的课程是 7天搞定Node.js微信公众号开发 这是自己结识 Node.js 以来的第二个7天的课程,但在之前还要对教程中涉及的 Koa 框架进行学习 Koa 介绍 koa 是由 Express 原班人马打造的,一个基于 Node.js 平台的 Web 开发框架 Express 是基于 ES5 的语法,随着新版 Node.js 开始支持 ES6 ,该团队重新编写诞生了 koa 1.0 ,而 koa2.0 则是更是超前的基于 ES7 安装 koa2 npm i koa 创建 koa2 工程 // 导入koa,和koa 1.x不同,在koa2中,我们导入的是一个class,因此用大写的Koa表示: const Koa = require('koa'); // 创建一个Koa对象表示web app本身: const app = new Koa(); app.use( ... ) // 在端口3000监听: app.listen(3000); app.use([path,] function [, function…]) 在

How to serve the same static files regardless of the route?

六眼飞鱼酱① 提交于 2019-12-24 11:34:59
问题 I have a react App. With webpack I build for production. Now I try to set a little koa server to serve the static files generated by webpack for production. So I did this import Koa from 'koa' import serve from 'koa-static' const app = new Koa() app.use(serve('dist/')) app.listen(3001) Where dist/ is the directory where are the files (index.html, bundle etc). This works well but only for the route '/' (localhost:3001/) In my app I use react router so I need to go to /login (localhost:3001

数据解析

旧城冷巷雨未停 提交于 2019-12-23 03:47:28
GET GET数据的处理:GET一般都是请求页面,不过也可以传传form表单,一起看看 html代码 <!DOCTYPE html> < html lang = " zh " > < head > < meta charset = " UTF-8 " > < title > Page Title </ title > </ head > < body > < form action = " http://localhost:7000 " method = " GET " > 用户名: < input type = " text " name = " username " > < br > 密码: < input type = " password " name = " userpassword " > < br > < button > 提交 </ button > </ form > </ body > </ html > 上传的数据在ctx.query对象中:ctx.query const Koa = require ( "koa" ) ; let server = new Koa ( ) ; server . listen ( 7000 ) ; server . use ( async ctx => { console . log ( ctx . query ) ; ctx