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)
相关推荐
donecoding3 小时前
3 条命令搞定闭环 Monorepo:Lerna 版本管理 + 拓扑构建 + 自定义分发
前端·前端框架·node.js
Flynt1 天前
npm v12 来了:allowScripts 默认关闭,我的项目差点跑不起来
安全·npm·node.js
森鹿1 天前
express中间件原理以及大致实现
前端·express
叫我Paul就好2 天前
尝试 Node 搭建后端-开发框架
node.js
风止何安啊4 天前
网课倍速痛点解决:一套前端代码实现自由控速播放器
前端·javascript·node.js
糖拌西瓜皮4 天前
Node.js核心模块实战:文件、路径、HTTP与流处理
javascript·node.js
糖拌西瓜皮4 天前
Node.js工程化实践:包管理、TypeScript配置与代码质量
typescript·node.js
糖拌西瓜皮4 天前
NestJS入门指南:Java开发者的Spring Boot体验
javascript·node.js
糖拌西瓜皮4 天前
Express框架快速上手:中间件、路由与错误处理
javascript·node.js
半个落月4 天前
从 Tokenization 到 Embedding:用 Node.js 搞懂大模型为什么先“分词”再“向量化”
人工智能·node.js