Node.js Express中使用joi进行表单验证

使用joi npm包可以很方便的在Node.js Express项目中实现表单验证,以下例子可供参考:

创建登录表单验证:

javascript 复制代码
const joi = require('joi')

const title = joi.string().min(1).max(45).required()//最少1位,最多18位,必选
const text = joi.string().max(999).required()//密码为1-18位任意字符
const category = joi.string().min(2).max(2).required()//长度为2位任意字符
const date = joi.string().min(19).max(19).required()//密码为1-18位任意字符

//创建文章表单验证
exports.createArticle_schema = {
    body:{
        title,
        text,
        category,
        date,
    },
}

使用表单验证:

javascript 复制代码
const expressJoi = require('@escook/express-joi')
const {createArticle_schema} = require('../schema/article')


router.post('/article', expressJoi(createArticle_schema), article_handler.createArticle)
相关推荐
Q_Q196328847527 分钟前
python+uniapp基于微信小程序的助眠小程序
spring boot·python·小程序·django·flask·uni-app·node.js
孟陬42 分钟前
事件驱动 vs 轮询:为什么 Node.js 官方推荐 `fs.watch()` 而非 `fs.watchFile`
node.js
林希_Rachel_傻希希3 小时前
Express 入门全指南:从 0 搭建你的第一个 Node Web 服务器
前端·后端·node.js
Q_Q51100828517 小时前
python+uniapp基于微信小程序团购系统
spring boot·python·微信小程序·django·uni-app·node.js·php
云枫晖1 天前
深入浅出npm:现代JavaScript项目基石
前端·javascript·node.js
Json____1 天前
使用node Express 框架框架开发一个前后端分离的二手交易平台项目。
java·前端·express
Q_Q19632884751 天前
python+vue的在线租房 房屋租赁系统
开发语言·vue.js·spring boot·python·django·flask·node.js
不会写DN1 天前
用户头像文件存储功能是如何实现的?
java·linux·后端·golang·node.js·github
前端双越老师1 天前
译: 构建高效 AI Agent 智能体
前端·node.js·agent
哆啦A梦15882 天前
搜索页面布局
前端·vue.js·node.js