登录ApiDoc官网,熟悉官方文档 ApiDoc官网
由官网所知,我们使用WebStorm创建好Express项目后,需要安装一个ApiDoc库,代码如下:
npm i apidoc -g #全局安装
{
"name": "node版前台接口文档",
"version": "0.0.1",
"description": "前台接口",
"title": "Node version foreground interface document",
"url" : "http://localhost:3000/api/index"
}
{
…..
"apidoc": {
"title": "接口文档",
"url": "http://localhost:3000"
}
}
/*
@api {post} /login 用户登录
@apiDescription 用户登录
@apiName login
@apiGroup IndexApi
@apiParam {string} username 用户名
@apiParam {string} password 密码
@apiSuccess {json} result
@apiSuccessExample {json} Success-Response:
{
"success" : "true",
"result" : {
"username" : "用户名",
"password" : "密码"
}
}
@apiSampleRequest http://localhost:3000/api/index/login
@apiVersion 0.0.1
*/
router.post('/login', function(req, res, next) {
let username = req.body.username;
let password = req.body.password;
//判断是否存在当前字段
if(username&&password){
//判断是否为空
if(username.trim()==""){
res.status(200).json({ code: 0, msg: '用户名不能为空' });
}else if(password.trim()==""){
res.status(200).json({ code: 0, msg: '密码不能为空' });
}else{
let value = [username];
let sql = '';
mysql.doGetQueryMain(sql,value,function(err,result){
if (err) {
throw err;
console.log("Error:" + err);
} else {
// console.log(result)
if(result.length==0){
res.status(200).json({ code: 0, msg: '用户名不存在'});
}else if(result.length>1){
res.status(200).json({ code: 0, msg: '登陆异常' });
}else if (result=== md5(password)) {
// 成功返回
res.status(200).json({ code: 1, msg: '登陆成功', result: { }});
}else{
res.status(200).json({ code: 0, msg: '密码不正确' });
}
}
});
}
}else{
res.status(200).json({ code: 0, msg: '参数不正确' });
}
});
项目执行命令行:
apidoc -i routes/ -o public/apidoc/
生成成功后,我们就可以测试运行api文档了,访问路径:
http://localhost:3000/apidoc/index.html
至此成功运行
本文地址:https://www.zhuimengzhu.com/details/139.html
转载地址:暂无
转载说明:转载时请在文首注明来源zhuimengzhu.com 及教程作者,并附本文链接。谢谢各位编辑同仁配合。zhuimengzhu 保留追究相应责任的权利。