Node.JS用純JavaScript生成圖片或滑塊式驗(yàn)證碼功能
有一些Node.JS圖片生成類(lèi)庫(kù),比如node-captcha等的類(lèi)庫(kù),需要c/c++程序生成圖片??缙脚_(tái)部署不是很方便。這里介紹幾個(gè)用純JS實(shí)現(xiàn)的圖片驗(yàn)證碼生成模塊。
captchapng
用純JavaScript實(shí)現(xiàn)的驗(yàn)證碼生成模塊。
https://github.com/GeorgeChan/captchapng
安裝簡(jiǎn)單,依賴(lài)少:
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è)計(jì)的驗(yàn)證碼生成模塊。
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')
})
前端滑塊驗(yàn)證
前端生成軌跡發(fā)送到后端驗(yàn)證,輸入簡(jiǎn)單,但是容易被破解。
https://gitee.com/LongbowEnterprise/SliderCaptcha
總結(jié)
以上所述是小編給大家介紹的Node.JS用純JavaScript生成圖片或滑塊式驗(yàn)證碼功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
node.js中stream流中可讀流和可寫(xiě)流的實(shí)現(xiàn)與使用方法實(shí)例分析
這篇文章主要介紹了node.js中stream流中可讀流和可寫(xiě)流的實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了node.js stream流可讀流和可寫(xiě)流基本分類(lèi)、原理、定義、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-02-02
詳解使用vscode+es6寫(xiě)nodejs服務(wù)端調(diào)試配置
本篇文章主要介紹了使用vscode+es6寫(xiě)nodejs服務(wù)端調(diào)試配置,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
詳解基于 Node.js 的輕量級(jí)云函數(shù)功能實(shí)現(xiàn)
這篇文章主要介紹了詳解基于 Node.js 的輕量級(jí)云函數(shù)功能實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
NodeJS前端自動(dòng)化部署實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了NodeJS前端自動(dòng)化部署實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
使用puppeteer破解極驗(yàn)的滑動(dòng)驗(yàn)證碼
這篇文章主要介紹了利用puppeteer破解極驗(yàn)的滑動(dòng)驗(yàn)證功能,基本流程代碼實(shí)現(xiàn)給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-02-02
淺談Node.js ORM框架Sequlize之表間關(guān)系
下面小編就為大家?guī)?lái)一篇淺談Node.js ORM框架Sequlize之表間關(guān)系。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

