koa

Wait for async process to finish before redirect in koajs

若如初见. 提交于 2019-12-10 00:42:45
问题 I'm currently trying to spawn a child process to handle some POST data with NodeJS (using Koa framework). Ideally, I would like to wait for the child process to finish before redirecting, but since the child process is asynchronous, the code always redirects first. I've been trying to fix this for a long time and came up with a couple hackish ways to partially solve it, but nothing very clean or usable. What is the best way to handle this? Below is the function for my post route (using koa

koa

一个人想着一个人 提交于 2019-12-09 13:13:51
koa 什么是koa koa:基于Node.js平台的下一代web开发框架 koa是express原班人马打造的 安装 npm i koa -S 如何使用 简单的打印 hello world let Koa =require('koa') let app=new Koa() app.use(async (ctx,next)=>{ ctx.body='hello world' }) app.listen(9000,()=>{ console.log('服务开启成功,端口9000') }) 中间件 什么是中间件? ·中间件是一个个 async await函数 app.use(async (ctx,next)=>{ //对于ctx和next详解 ctx:是对请求体和响应体的封装 1.ctx.response.body 响应体 简写 ctx.body 2.ctx.request.url 获取请求路径 简写 ctx.url 3.ctx.path 得到的是去掉参数之后的路径 如路径是/list?id=0 得到后的是 /list next:是执行下一个中间件的用的 }) 处理静态资源 用法: app.use(static(path.join(process.cwd()))) process.cwd()和__dirname的区别 process.cwd():是当前进程的工作目录 __dirname

一张图掌握移动Web前端所有技术(大前端、工程化、预编译、自动化)

主宰稳场 提交于 2019-12-09 10:42:09
你要的移动web前端都在这里! 大前端方向:移动Web前端、Native客户端、Node.js、 大前端框架:React、Vue.js、Koa 跨终端技术:HTML 5、CSS 3、JavaScript 跨平台框架:React Native、Cordova 前端工程化:Grunt、Gulp、Webpack 前端预编译:Babel、Sass、Less 自动化测试:Jasmine、Mocha、Karma 一图在手,应有尽有! 更多信息参考: 来源: oschina 链接: https://my.oschina.net/u/59463/blog/1539543

Does Koa safely handle errors?

老子叫甜甜 提交于 2019-12-08 21:01:46
The Nodejs API states... By the very nature of how throw works in JavaScript, there is almost never any way to safely "pick up where you left off", without leaking references, or creating some other sort of undefined brittle state. However Koa traps errors and avoids exiting the nodejs process. What enables Koa to safely flout this advice? There are primarily two cases where koa can not safely handle errors. Thrown errors on different ticks: app.use(function* () { setImmediate(function () { throw new Error('boom') }) }) Emitter errors that are not set as response.body= : app.use(function* () {

Does Koa safely handle errors?

前提是你 提交于 2019-12-08 04:55:34
问题 The Nodejs API states... By the very nature of how throw works in JavaScript, there is almost never any way to safely "pick up where you left off", without leaking references, or creating some other sort of undefined brittle state. However Koa traps errors and avoids exiting the nodejs process. What enables Koa to safely flout this advice? 回答1: There are primarily two cases where koa can not safely handle errors. Thrown errors on different ticks: app.use(function* () { setImmediate(function (

Koa2 之文件上传下载的示例代码

走远了吗. 提交于 2019-12-07 21:36:34
上传下载在 web 应用中还是比较常见的,无论是图片还是其他文件等。在 Koa 中,有很多中间件可以帮助我们快速的实现功能。 文件上传 在前端中上传文件,我们都是通过表单来上传,而上传的文件,在服务器端并不能像普通参数一样通过 ctx.request.body 获取。我们可以用 koa-body 中间件来处理文件上传,它可以将请求体拼到 ctx.request 中。 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // app.js const koa = require( 'koa' ); const app = new koa(); const koaBody = require( 'koa-body' ); app.use(koaBody({ multipart: true , formidable: { maxFileSize: 200*1024*1024 // 设置上传文件大小最大限制,默认2M } })); app.listen(3001, ()=>{ console.log( 'koa is listening in 3001' ); }) 使用中间件后,就可以在 ctx.request.body.files 中获取上传的文件内容。需要注意的就是设置 maxFileSize,不然上传文件一超过默认限制就会报错。 接收到文件之后

When should I use yield and when yield* with Koa.js

半腔热情 提交于 2019-12-07 17:54:01
问题 Browsing through koa samples, docs and middleware, I noticed both forms of yield are being used without any particular difference i noticed. The most extreme case is in koa-mount, where the sample code uses the yield next; form, and the package itself uses yield* several times. Other packages (koa-views for example) also use the yield next form. I understand the difference between the 2 forms as defined by the language, but don't understand how is it that in the context of koa they're used

strapi - restrict user to fetch only data related to him

大兔子大兔子 提交于 2019-12-07 09:31:51
问题 Usually, a logged-in user gets all entries of a Content Type. I created a "snippets" content type (_id,name,content,users<<->>snippets) <<->> means "has and belongs to many" relation. I created some test users and make a request: curl -H 'Authorization: Bearer eyJ...' http://localhost:1337/snippets/ Main Problem: an authenticated user should only see the entries assigned to him. Instead, a logged-in user gets all snippets, which is bad. How is it possible to modify the fetchAll(ctx.query);

OpenShift not working with certain Nodejs dependencies (Koa)

不问归期 提交于 2019-12-07 06:58:17
问题 I've checked out How to setup KoaJS in Openshift and it's still not working. Here is a part of my package.json file: "engines": { "node": ">= 0.12.0", "npm": ">= 1.0.0" }, "dependencies": { "co-busboy": "^1.3.0", "forever": "^0.14.1", "fs": "0.0.2", "koa": "^0.18.1", "koa-logger": "^1.2.2", "koa-router": "^4.2.0", "koa-static": "^1.4.9", "path": "^0.11.14" }, "devDependencies": {}, "bundleDependencies": [], "private": true, "main": "--harmony app.js" And then to my app.js file. This code

[转]nodeJs--koa2 REST API

余生长醉 提交于 2019-12-07 06:43:08
本文转自: https://blog.csdn.net/davidPan1234/article/details/83413958 REST API规范 编写REST API,实际上就是编写处理HTTP请求的async函数,不过,REST请求和普通的HTTP请求有几个特殊的地方: REST请求仍然是标准的HTTP请求,但是,除了GET请求外,POST、PUT等请求的body是JSON数据格式,请求的Content-Type为application/json; REST响应返回的结果是JSON数据格式,因此,响应的Content-Type也是application/json。 1、工程结构 2、目录详解 package.json:项目描叙 { "name": "rest-koa", "version": "1.0.0", "description": "rest-koa project", "main": "app.js", "scripts": { "dev": "node --use_strict app.js" }, "keywords": [ "koa", "rest", "api" ], "author": "david pan", "dependencies": { "koa": "2.0.0", "koa-bodyparser": "3.2.0", "koa