【Node + svg-captcha】生成图片验证码

本文详细介绍了如何使用Node.js和svg-captcha库生成并验证SVG图片验证码。通过配置Express、express-session和svg-captcha,实现了验证码的生成、存储及验证过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【node + svg-captcha】生成图片验证码

步骤

  1. 安装
npm install express express-session svg-captcha --save
  1. 创建
// 引入需要用到的包
const express = require('express')
const app = express()
const session = require('express-session')
const svgCaptcha = require('svg-captcha')

// 将session挂载到app实力中,后面验证的时候用得到
app.use(session({
	secret: 'Keyboard cat',
	resave: false,
	saveUninitialized: true,
	cookie: {
		secure: false,
		maxAge: 1000 * 5
	},
	rolling: true
}))

// 定义生成验证码函数
function createCode () {
	const colorMap = ['#eeeeee', '#edfedf', '#eeddff', 'skyblue', 'orange', '#c8c8c8'] // 配置背景图片颜色集合
	const randomColor = colorMap[Math.floor(Math.random() * colorMap.length)] //随机颜色
	const  options ={
		size: 4, // 验证码长度
		ignoreChars: '0oO1lI', // 排除字符
		noise: 2, // 干扰条数
		width: 160,
		height: 50,
		fontSize: 50,
		color: true,
		background: randomColor
	}
	const captcha = svgCaptcha.create(options)
	console.log(captcha)
	return captcha
}

// createCode()
  1. 返回响应
// 上面已经拿到了验证码以及svg验证码图片,可以配置路由了
app.get('/api/captcha', (req, res) => {
	const captchaObj = createCode()
	req.session.captcha = captchaObj.text.toLowerCase() // 将验证码的值存入session
	res.setHeader('content-Type', 'svg') // 响应类型设置为svg
	res.send(captchaObj.data)
})

// 验证验证码是否正确
app.get('/api/verify', (req, res) => {
	const serverCode = req.session.captcha.toLowerCase()
	const clientCode = req.query.code.toLowerCase()
	if (serverCode === clientCode) {
		res.json({status: 200, isTrue: true})
	} else {
		res.json({status: 500, isTrue: false})
	}
})

app.listen(3000, ()=> {
	console.log('your app is running at http://localhost:3000')
})

完整代码如下

// 引入需要用到的包
const express = require('express')
const app = express()
const session = require('express-session')
const svgCaptcha = require('svg-captcha')

// 定义生成验证码函数
function createCode () {
	// 配置背景图片颜色集合
	const colorMap = ['#eeeeee','#edfedf','#eeddff','skyblue','orange','#c8c8c8'] 
	
	//随机颜色
	const randomColor = colorMap[Math.floor(Math.random() * colorMap.length)]
	
	const  options ={
		size: 4, // 验证码长度
		ignoreChars: '0oO1lI', // 排除字符
		noise: 2, // 干扰条数
		width: 160,
		height: 50,
		fontSize: 50,
		color: true,
		background: randomColor
	}
	const captcha = svgCaptcha.create(options)
	console.log(captcha)
	return captcha
}

app.use(session({
	secret:'Keyboard cat',
	resave: false,
	saveUninitialized: true,
	cookie: {
		secure: false,
		maxAge: 30000
	},
	rolling: true
}))

// createCode()
app.get('/api/captcha', (req, res) => {
	const captchaObj = createCode()
	req.session.captcha = captchaObj.text.toLowerCase()
	res.setHeader('Content-Type', 'text/html');
	res.send(captchaObj.data)
})

app.get('/api/verify', (req, res) => {
	const serverCode = req.session.captcha.toLowerCase()
	const clientCode = req.query.code.toLowerCase()

	if (serverCode === clientCode) {
		res.json({status: 200, isTrue: true})
	} else {
		res.json({status: 500, isTrue: false})
	}
})


app.listen(3000, ()=> {
	console.log('your app is running at http://localhost:3000')
})

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值