定义app.post()中的回调函数
2017-02-03 15:18
316 查看
参考 express官网:
You can provide multiple callback functions that behave just like middleware, except that these callbacks can invoke
on a route, then pass control to subsequent routes if there’s no reason to proceed with the current route.
为了提高代码的简洁性,我们经常把回调函数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;
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;
相关文章推荐
- 关于iOS NSOperation 自定义的那些事儿
- 【Android 进阶】图片加载框架之Glide
- Android全屏Dialog
- 【内存泄漏】Android内存泄漏---单例内存泄漏
- iOS7中的ViewController切换
- Android硬件访问服务---APK编写
- Android demo-->学生选课系统,使用Sqlite存储数据
- Android APK反编译就这么简单 详解(附图)
- 19-io_文件权限掩码_动态库_静态库
- IOS10 Notification推送通知(一)
- Android之沉浸式状态栏的实现方法、状态栏透明
- ios在同一个控制器中 动态显示和隐藏状态栏
- 创建第一个微信小程序
- OFFTIME for Android 断线时间 手机想都别想
- 简单封装微信分享插件
- android 播放R文件音乐
- Android DataBinding库(MVVM设计模式)
- unity3d状态机基础学习(二)
- 什么是傅里叶变换,本质是?
- Android Studio Debug调试