Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report