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)
相关推荐
艾小码9 小时前
手把手教你实现一个EventEmitter,彻底告别复杂事件管理!
前端·javascript·node.js
前端小哲11 小时前
MCP从入门到实战
node.js·ai编程
dasseinzumtode12 小时前
nestJS 使用ExcelJS 实现数据的excel导出功能
前端·后端·node.js
梅孔立17 小时前
服务器不支持node.js16以上版本安装?用Docker轻松部署Node.js 20+环境运行Strapi项目
服务器·docker·node.js
XiaoMu_00117 小时前
基于Node.js和Three.js的3D模型网页预览器
javascript·3d·node.js
卿·静17 小时前
Node.js对接即梦AI实现“千军万马”视频
前端·javascript·人工智能·后端·node.js
lvlv_feifei17 小时前
N8N macOS (Apple Silicon) 完整安装配置教程
node.js·workflow
Cosmoshhhyyy1 天前
Node.js 18+安装及Claude国内镜像使用、idea中claude插件下载指南
node.js
徐_三岁1 天前
关于npm的钩子函数
前端·npm·node.js
不买Huracan不改名2 天前
安装Codex(需要用npm)
前端·npm·node.js