核心提示:node 使用svg-captvcha模块主要代码//登录验证码app.get(/api/captcha, function (req, res) {var captcha = svgCaptcha....
node 使用svg-captvcha模块
主要代码
//登录验证码
app.get('/api/captcha', function (req, res) {
var captcha = svgCaptcha.create();
// req.session.captcha = captcha.text;
res.type('application/json');
res.status(200).send({
svg: captcha.data,
answer: captcha.text,
});
});
react前端显示
getvertification() {
fetch('/api/captcha', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
.then(data => {
// tslint:disable-next-line:no-console
return data.json();
}).then(mjson => {
// tslint:disable-next-line:no-console
console.log(mjson);
this.setState({ verification_data: mjson.svg, answer: mjson.answer});
setTimeout(() => {
if (this.refs.captcha_img) {
(this.refs.captcha_img as HTMLDivElement).innerHTML = mjson.svg;
}
}, 100);
// if(verification === ansewer)
});
}


