Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use pure JavaScript to generate picture or slider CAPTCHA function for Node.JS

Shulou Source: shulou.com Published: 2022-06-01 01:57:09 10月03日 Update

This article introduces the "Node.JS how to use pure JavaScript to generate pictures or slider CAPTCHA function" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

There are some Node.JS picture generation libraries, such as node-captcha and so on, which require the cAccord Clipper + program to generate pictures. Cross-platform deployment is not very convenient. This paper introduces several picture CAPTCHA generation modules realized by pure JS.

Captchapng

CAPTCHA generation module realized by pure JavaScript.

Https://github.com/GeorgeChan/captchapng

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.

Https://github.com/napa3um/node-captcha

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.

This is the end of the content of "how Node.JS uses pure JavaScript to generate pictures or slider CAPTCHA". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Generation verification pictures modules functions content front end more knowledge examples practicality learning and then dilemma reality situation articles frameworks cases programs Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei Microsoft Shulou Information MySQL