Node.JS用純JavaScript生成圖片或滑塊式驗證碼功能
有一些Node.JS圖片生成類庫,比如node-captcha等的類庫,需要c/c++程序生成圖片。跨平臺部署不是很方便。這里介紹幾個用純JS實現(xiàn)的圖片驗證碼生成模塊。
captchapng
用純JavaScript實現(xiàn)的驗證碼生成模塊。
https://github.com/GeorgeChan/captchapng
安裝簡單,依賴少:
npm install captchapng
示例:
var captchapng = require('captchapng'); app.get('/sign/captcha.png', function(req, res) { var captchaNumber = parseInt(Math.random() * 9000 + 1000) req.session.captcha = captchaNumber var p = new captchapng(80,20, captchaNumber); // width,height,numeric captcha p.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
為Express框架設(shè)計的驗證碼生成模塊。
https://github.com/napa3um/node-captcha
安裝&示例:
$ npm install captcha Usage (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(` <img src="${ captchaUrl }"/> <form action="/login" method="post"> <input type="text" name="${ captchaFieldName }"/> <input type="submit"/> </form> `) }) app.post('/login', (req, res) => { res.type('html') res.end(` <p>CAPTCHA VALID: ${ captcha.check(req, req.body[captchaFieldName]) }</p> `) }) app.listen(8080, () => { console.log('server started') })
前端滑塊驗證
前端生成軌跡發(fā)送到后端驗證,輸入簡單,但是容易被破解。
https://gitee.com/LongbowEnterprise/SliderCaptcha
總結(jié)
以上所述是小編給大家介紹的Node.JS用純JavaScript生成圖片或滑塊式驗證碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
詳解nodejs微信公眾號開發(fā)——2.自動回復(fù)
這篇文章主要介紹了詳解nodejs微信公眾號開發(fā)——2.自動回復(fù),非常具有實用價值,需要的朋友可以參考下2017-04-04node.js中的buffer.toString方法使用說明
這篇文章主要介紹了node.js中的buffer.toString方法使用說明,本文介紹了buffer.toString的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下2014-12-12Node.js中常用設(shè)計模式的使用方法總結(jié)
設(shè)計模式是由經(jīng)驗豐富的程序員在日積月累中抽象出的用以解決通用問題的可復(fù)用解決方案,它提供了標(biāo)準(zhǔn)化的代碼設(shè)計方案提升開發(fā)體驗,本文主要來和大家討論一下Node.js中設(shè)計模式的重要性并提供一些代碼示例,感興趣的可以了解下2023-10-10淺析nodejs實現(xiàn)Websocket的數(shù)據(jù)接收與發(fā)送
WebSocket是HTML5開始提供的一種瀏覽器與服務(wù)器間進行全雙工通訊的網(wǎng)絡(luò)技術(shù),本文給大家介紹nodejs實現(xiàn)websocket的數(shù)據(jù)庫接收與發(fā)送,小伙伴們一起學(xué)習(xí)吧2015-11-11