定义app.post()中的回调函数
来源:互联网 发布:淘宝店买棉被店名 编辑:程序博客网 时间:2024/05/17 13:06
参考 express官网:
app.post(path, callback [, callback ...])
Routes HTTP POST requests to the specified path with the specified callback functions. For more information, see the routing guide.
You can provide multiple callback functions that behave just like middleware, except that these callbacks can invoke next('route')
to bypass the remaining route callback(s). You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there’s no reason to proceed with the current route.
app.post('/', function (req, res) { res.send('POST request to homepage');});
为了提高代码的简洁性,我们经常把回调函数function(req,res) 单独抽象出来
var fs =require('fs');var util=require('./../util');var MESSAGE_PATH='./database/meassage.json';var USER_PATH='./database/user.json';var Message={ init:function(app){ app.post('/message/get',this.getMessage); app.post('/message/add',this.addMessage); }, getMessage:function(req,res){ var key=req.param('key'); if(key!==util.getkey()){ return res.send({ status:0, data:'使用了没有鉴权的key' }) } fs.readFile(MESSAGE_PATH,function(err,data){ if(err){ return res.send({ status:0, err:err }) }else{ try{ var obj=JSON.parse(data); return res.send({ status:1, data:obj }) }catch(e){ return res.send({ status:0, err:e }) } } }) } addMessage:function(req,res){ var token=req.param('token'); var message=req.param('message'); if(!token||!message){ return res.send({ status:0, err:'token或者Message不能为空' }) } fs.readFile(USER_PATH,function(err,data){ if(err){ return res.send({ status:0, err:err }) } try{ var obj=JSON.parse(data); for(var i in obj){ if(obj[i].token===token){ var msgObj=JSON.parse(fs.readFileSynce(MESSAGE_PATH)); msgObj.push({ messageid:util.guid(), userid:obj[i].userid, username:obj[i].username, time:new Date().getFullYear()+'-'+(pareInt(new Date().getMonth())+1)+'-'+new Date().getDate(), message:message }); fs.writeFileSync(MESSAGE_PATH,JSON.stringify(msgObj)); return res.send({ status:1 }) } } return res.send({status:0, err:'token认证失败' }) }catch(e){ return res.send({status:0, err:e }) } }) }}module.exports=Message;
0 0
- 定义app.post()中的回调函数
- 回调函数定义
- jquery post()方法中的回调函数 不执行
- unity 定义回调函数
- $.post 回调函数无法执行
- jquery post回调函数不执行
- $.post(路径,参数,回调函数,数据类型);
- VC回调函数定义和使用
- VC回调函数定义和使用
- 定义成员函数为回调函数
- 回调函数定义和使用
- typedef定义函数,回调函数
- MFC 关于回调函数定义问题
- 回调函数的定义与用法
- #define定义回调函数用法
- JavaScript中定义回调函数
- 定义回调函数的方法
- PHP 中回调函数的定义
- solr facet浅谈
- BZOJ 1455: 罗马游戏 左偏树裸题
- urllib库的使用
- 1-系统移植_u-boot移植
- 使用keras实现简单的前向全连接神经网络
- 定义app.post()中的回调函数
- 2-内核的编译_uImag_zimage_设备树
- BeautifulSoup使用笔记
- Notifycation(通知)的使用
- ALSA Dynamic PCM
- DataGrip 使用技巧二
- Fleurdalis博客简介
- Linux Kernel media框架(二)
- IP地址设置里,默认网关是什么?