定义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