handler.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var Handler = {
  2. // getViewPath:function(path){
  3. // var request = URL.parse(VIEWSPATH+path,true);
  4. // var FileName = request.pathname+'.html';
  5. // if(FILE.existsSync(FileName)){
  6. // return FileName;
  7. // }else{
  8. // return null;
  9. // }
  10. // },
  11. // getView:function(path){
  12. // var FileName = this.getViewPath(path);
  13. // if(FileName){
  14. // return FILE.readFileSync(FileName,'utf8');
  15. // }else{
  16. // return null;
  17. // }
  18. // },
  19. response:function(res,data){
  20. if(!res) return;
  21. res.writeHead(200, {'Content-Type': 'text/html; charset=UTF-8'});
  22. res.write(data);
  23. res.end();
  24. },
  25. apiResponse:function(res,data){
  26. if(!res) return;
  27. data = data?{success:1,result:data}:{success:1};
  28. res.writeHead(200, {'Content-Type': 'text/json; charset=UTF-8'});
  29. res.write(JSON.stringify(data));
  30. res.end();
  31. },
  32. apiErrorResponse:function(res,msg){
  33. if(!res) return;
  34. msg = msg?{success:0,msg:msg}:{success:0};
  35. res.writeHead(200, {'Content-Type': 'text/json; charset=UTF-8'});
  36. res.write(JSON.stringify(msg));
  37. res.end();
  38. }
  39. }
  40. module.exports = Handler;