# 在/application/controllers/article.js中新增代码块 ``` async add(req, res) { if (req.isPost) { //提交后 var post = req.body; var title = post.title.trim(); //标题判断 if (!title) { res.render('msg', { code: 0, msg: '标题不能为空', url: '' }); return; } //内容判断 var content = post.content.trim(); if (!content) { res.render('msg', { code: 0, msg: '内容不能为空', url: '' }); return; } //执行插入并返回id let insertId = await model.i("INSERT INTO article SET ?", post); if (insertId) { res.render('msg', { code: 1, msg: '操作成功', url: '/article/index' }); } else { res.render('msg', { code: 0, msg: '操作失败', url: '' }); } return; } //加载模板文件 res.render(req.TPL, {}); } ``` # 新建对应模板文件/application/views/article/add.html ``` <!doctype html> <html> <head> <meta charset="utf-8"> <title>添加文章</title> </head> <body> <div style="text-align:center"> <h2>添加文章 <span><a href="/article">返回</a></span></h2> <form action="" method="post"> <table width="100%" border="1"> <tbody> <tr> <td align="right">标题</td> <td align="left"><input type="text" name="title" id="" value=""></td> </tr> <tr> <td align="right">内容</td> <td align="left"><textarea name="content" id="" cols="30" rows="10"></textarea></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="提交"></td> </tr> </tbody> </table> </form> </div> </body> </html> ``` # 显示结果 ![](https://img.kancloud.cn/ad/a9/ada91b2030281cfa13a75775f8fff7e5_895x346.png)