koa-2 实现验证码验证
首先安装验证码插件
这里推荐使用ccap,这个插件是我在寻找过程中,觉得能跟koa搭配比较好的一个插件,其他的要不就是需要express,要不就是需要安装其他一系列比较大的类库。
1 | npm <span class = "hljs-keyword" >install</span> ccap <span class = "hljs-comment" >--save</span> |
如何使用?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | const ccap = <span class = "hljs-built_in" >require</span>(<span class = "hljs-string" > 'ccap' </span>)(); home. get (<span class = "hljs-string" > '/home' </span>, <span class = "hljs-function" ><span class = "hljs-params" >(ctx, next)</span> =></span> { <span class = "hljs-keyword" > return </span> next().<span class = "hljs-keyword" >then</span>(<span class = "hljs-function" ><span class = "hljs-params" >()</span> =></span> { ctx.body = ctx.session.captcha; }); }); home. get (<span class = "hljs-string" > '/captcha' </span>, <span class = "hljs-function" ><span class = "hljs-params" >(ctx, next)</span> =></span> { <span class = "hljs-keyword" > return </span> next().<span class = "hljs-keyword" >then</span>(<span class = "hljs-function" ><span class = "hljs-params" >()</span> =></span> { let ary = ccap. get (); let txt = ary[<span class = "hljs-number" > 0 </span>]; let buf = ary[<span class = "hljs-number" > 1 </span>]; ctx.body = buf; ctx.type = <span class = "hljs-string" > 'image/png' </span>; ctx.session.captcha = txt; }); }); |
可以了,就是这么简单的。
版权声明
由 durban创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于
博客(
https://www.gowhich.com ),版权所有,侵权必究。
本文永久链接: https://www.gowhich.com/blog/720
版权声明
由 durban创作并维护的 Gowhich博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证。
本文首发于 Gowhich博客( https://www.gowhich.com ),版权所有,侵权必究。
本文永久链接: https://www.gowhich.com/blog/720