How to use pure JavaScript to generate picture or slider CAPTCHA function
This article mainly explains "how to use pure JavaScript to generate pictures or slider CAPTCHA function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use pure JavaScript to generate pictures or slider CAPTCHA function" it!
Captchapng
CAPTCHA generation module realized by pure JavaScript.
Easy to install and less dependent:
Npm install captchapng
Example:
Var captchapng = require ('captchapng'); app.get (' / sign/captcha.png', function (req, res) {var captchaNumber = parseInt (Math.random () * 9000 + 1000) req.session.captcha = captchaNumbervar p = new captchapng (80 mine20, captchaNumber); / / width,height,numeric captchap.color (0,0,0,0); / / First color: background (red, green, blue, alpha) p.color (80,80,80,255) / / Second color: paint (red, green, blue, alpha) var img = p.getBase64 (); var imgbase64 = new Buffer (img,'base64'); res.writeHead (200,{ 'Content-Type':' image/png'}); res.end (imgbase64);})
Express + Captcha
CAPTCHA generation module designed for Express framework.
Installation & example:
$npm install captchaUsage (for Express 4) 'use strict'const express = require (' express') const session = require ('express-session') const bodyParser = require (' body-parser') const captchaUrl ='/ captcha.jpg'const captchaId = 'captcha'const captchaFieldName =' captcha'const captcha = require ('. / captcha'). Create ({cookie: captchaId}) const app = express () app.use (session ({secret: 'keyboard cat',resave: false,saveUninitialized: true) }) app.use (bodyParser.urlencoded ({extended: false})) app.get (captchaUrl, captcha.image ()) app.get ('/', (req, res) = > {res.type ('html') res.end (`
`)}) app.post ('/ login', (req, res) = > {res.type ('html') res.end (`
CAPTCHA VALID: ${captcha.check (req, req.body [captchaFieldName])}
`)}) app.listen (8080, () = > {console.log ('server started')})
Front-end slider verification
The front-end generation track is sent to the back-end verification, the input is simple, but it is easy to crack.
Thank you for your reading, the above is "how to use pure JavaScript to generate pictures or slider CAPTCHA function" of the content, after the study of this article, I believe you on how to use pure JavaScript to generate pictures or slider CAPTCHA function of this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!