koa

Node.js Web开发(二)

橙三吉。 提交于 2019-12-23 01:00:11
上一篇文章主要介绍了koa2的基本操作(坐下坐下),但是每次都返回一样的HTML似乎也不可能,所以现在我们需要来处理URL了。 直接来点高级点的东西,我们需要一个能处理URL的middleware,它叫做: koa-router : npm install koa-router 1234567891011121314151617181920 const Koa = require('koa');const router = require('koa-router')();const app = Koa();router.get('/hello/:name', async(ctx, next) => { var name = ctx.params.name; ctx.response.body = `<h1>Hello, {$name}!</h1>`;});router.get('/', async(ctx, next) => { ctx.response.body = '<h1>Index Page</h1>';});app.use(router.routes());app.listen(3000);console.log('Server listen on port 3000...'); 这里的代码,会在访问网址localhost:3000时返回Index Page

koa项目搭建

痴心易碎 提交于 2019-12-23 00:39:25
npm install -g koa-generator 全局安装“koa-generator”脚手架 koa2 -e '项目名’创建模板, SET DEBUG=koa* & npm start koa-learn 启动代码 npm install 安装依赖 2.async await 3.中间件 4.路由和cookie 来源: CSDN 作者: RuyiY 链接: https://blog.csdn.net/weixin_43824571/article/details/103653736

6. Koa中的错误处理

倖福魔咒の 提交于 2019-12-22 21:36:15
1. Koa中自带的错误处理   koa中,通过ctx.throw( 状态码 )来抛出异常, 也可以携带自己想提示错误信息  //这里我们限制输入的id不能为负数,否则就抛出错误 usersRouter.get('/:id', (ctx) => { if(ctx.params.id * 1 < 0 ) { ctx.throw(404, '这个用户太小了, 找不到') } ctx.body = db[ctx.params.id * 1] })             图1 koa自带错误处理结果   从上图我们可以看出, 返回的状态码为404, 错误处理信息也显示了。但是这不是我们想要的, 在Restful API的规定中, 我们尽量使用json格式返回信息   所以我们可以自己写一个中间件, 放在执行顺序的最前面, 来对后面执行的代码进行错误处理 //错误处理中间件 app.use(async (ctx, next) => { try { await next() } catch (error) { ctx.status = error.status || error.statusCode ctx.body = { message: error.message } } })     可以看到, 错误信息就以Json的格式显示出来了 2. 使用中间件koa-json

Mount Koajs app on top of Express

血红的双手。 提交于 2019-12-22 11:10:27
问题 From koajs.com: app.callback() Return a callback function suitable for the http.createServer() method to handle a request. You may also use this callback function to mount your koa app in a Connect/Express app. Now I have an Express app that already starts its own http server. How can I mount a koa app on top of this existing server, so that it shares the same port? Would I include the koa app as an Express middlware? Do I still use app.callback() for that? 回答1: expressapp.use(koaapp.callback

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

回眸只為那壹抹淺笑 提交于 2019-12-19 07:41:37
问题 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. 回答1: Reading from cloud functions doc: Base Image Cloud Functions uses a Debian-based

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

心不动则不痛 提交于 2019-12-19 07:41:12
问题 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. 回答1: Reading from cloud functions doc: Base Image Cloud Functions uses a Debian-based

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

半世苍凉 提交于 2019-12-19 03:47:05
问题 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. 回答1: I'm still a beginner, so it's likely that a lot of this can be optimised

Koa 中的错误处理

馋奶兔 提交于 2019-12-17 22:48:50
不像 express 中在末尾处注册一个声明为 (err, req, res, next) 中间件的方式,koa 刚好相反,在开头进行注册。 app.use(async (ctx, next) => { try { await next(); } catch (err) { ctx.status = err.status || 500; ctx.body = err.message; ctx.app.emit("error", err, ctx); } }); 这样程序中任何报错都会收敛到此处。此时可以方便地将错误打印到页面,开发时非常便捷。 + ctx.app.emit('error', err, ctx); koa 也建议通过 app 来派发错误,然后通过监听 app 上的 error 事件对这些错误做进一步的统一处理和集中管理。 app.on("error", (err, ctx) => { /* 错误的集中处理: * log 出来 * 写入日志 * 写入数据库 * ... */ }); 一个错误捕获并打印到页面的示例: const Koa = require("koa"); const app = new Koa(); app.use(async (ctx, next) => { try { await next(); } catch (err) { const

What happens when promise is yielded in javascript?

半城伤御伤魂 提交于 2019-12-17 13:22:58
问题 Didn't find full answer .. What happens when promise is yielded? Is such construction var p = new Promise() p.resolve(value) function * (){ yield p } equivalent to function * (){ yield value } ? UPDATE How to mix different styles of async programming, for example for such framework as koa? Koa middlewares are working with generators, but there are lots of good packages which are promise-based (sequelize for ex.) 回答1: As Felix says, a promise is just another value to be yielded. However, there

大学我都是自学走来的,这些私藏的实用工具/学习网站我贡献出来了,建议收藏精品推荐

人盡茶涼 提交于 2019-12-16 13:38:59
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 作者 | Jeskson 来源 | 达达前端小酒馆 1 https://www.h5jun.com/archives/ 十年踪迹的博客 2 https://www.zhangxinxu.com/ 3 http://www.ruanyifeng.com/home.html 4 https://www.cnblogs.com/yexiaochai/ 5 https://www.cnblogs.com/rubylouvre/ 6 https://www.cnblogs.com/TomXu/ 7 https://www.liaoxuefeng.com/ 8 https://jiongks.name/ 网站结构 这里有: 我的技术心得、 demo演示、 幻灯片演示等内容; 当然也会有一些: 生活点滴、 兴趣爱好、 个人观点; 外加一些: 翻译大家不熟悉的好文章; 这里比较少有: 照片和视频; 这里不会有: 无脑转载内容 大概的人生规划是: 20 岁~ 30 岁写代码 30 岁~ 40 岁玩音乐 40 岁~ 50 岁当足球教练 听说程序员的平均寿命是 47.5 岁…… 如果我还能活到 50 岁,就给自己写一篇个人自传——只写给我自己 如果我还能活到 60 岁,就把 50 岁写的书出版 总之我就是这么爱幻想…… 9