【Node.js】之第三方中介軟體實現驗證碼
阿新 • • 發佈:2021-01-08
技術標籤:ES6&Nodejavascriptnode.jses6
Node.js第三方中介軟體實現驗證碼
svg-captcha驗證碼
使用:
(1)下載
npm i svg-captcha --save
(2)引入
不用app.use
const svgCaptcha=require("svg-captcha")
(3)建立一個ico
let svgico =svgCaptcha.create()
// res.type("svg") 設定渲染的資料的型別
/*
可以傳一個物件作為引數
let svgico= svgCaptcha.create({
size:6,//驗證碼個數
ignoreChars:"0o1Il",//忽略的字元
noise:10,//幾根線
color:false,//字型是否有顏色
background:"red",//背景色
})
*/
(4)程式碼例項
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
</head>
<body>
< form action="" method="get">
驗證碼:<img src="http://localhost:3000/login" alt="12" width="300" height="100">
</form>
</body>
</html>
verification.js
const express = require('express');
const svgCaptcha = require('svg-captcha' );
let app = express();
app.get("/login", (req, res) => {
let svgico = svgCaptcha.create({
size: 6,
ignoreChars: "0o1I1",
noise: 5,
color: true,
background: "#eee"
})
// console.log(svgico);
/*{
text 給後端去驗證
data 給前端顯示
}
create可以接收一個物件作為引數
{
size:6,//驗證碼個數
ignoreChars:"0o1Il",//忽略的字元,不會出現在驗證碼裡
noise:10,//幾根線
color:false,//字型是否有顏色
background:"red",//背景色
}
*/
res.type("svg"); // 單獨設定檔案型別
res.send(svgico.data)
})
app.listen(3000, () => {
console.log("Port 3000 is listening...");
})