static.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict'
  2. //TODO Rewrite
  3. var Static = {
  4. load:function(assetFile,req,res){
  5. let ext = Path.extname(assetFile);
  6. if(!ext||ext==''){
  7. res.writeHead(404, {'Content-Type': 'text/html'});
  8. res.end();
  9. return;
  10. }
  11. if(ext.match(/png|jpg|jpeg|gif/)){
  12. // res.writeHead(404, {'Content-Type': 'text/html'});
  13. // res.end();
  14. IMAGE.load(URL.parse(assetFile,true).pathname,ext,req,res);
  15. }else if(FILE.existsSync(assetFile)){
  16. var data;
  17. if(ext.match(/txt|js|css|html|json/)){
  18. try{
  19. data = FILE.readFileSync(assetFile,'utf-8');
  20. }catch(err){
  21. res.writeHead(404, {'Content-Type': 'text/html'});
  22. res.end();
  23. return false;
  24. }
  25. if(ext.match(/json/)){
  26. res.writeHead(200, { "Content-Type": MIME.lookup(ext)+';charset=utf-8'});
  27. res.end(data,'utf-8');
  28. }else{
  29. res.writeHead(200, { "Content-Type": MIME.lookup(ext)+';charset=utf-8','Content-Encoding':'gzip'});
  30. res.end(data,'utf-8');
  31. }
  32. //console.log(query)
  33. }else if(ext.match(/json/)){
  34. }else{
  35. try{
  36. data = FILE.readFileSync(assetFile,'binary');
  37. res.writeHead(200, { "Content-Type": MIME.lookup(ext),"Access-Control-Allow-Origin":"*"});
  38. res.end(data,'binary');
  39. }catch(err){
  40. res.writeHead(404, {'Content-Type': 'text/html'});
  41. res.end();
  42. }
  43. }
  44. }else{
  45. res.writeHead(404, {'Content-Type': 'text/html'});
  46. res.end();
  47. }
  48. }
  49. }
  50. module.exports=Static;