static.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. IMAGE.load(URL.parse(assetFile,true).pathname,ext,req,res);
  13. }else if(FILE.existsSync(assetFile)){
  14. var data;
  15. if(ext.match(/txt|js|css|html|json/)){
  16. try{
  17. data = FILE.readFileSync(assetFile,'utf-8');
  18. }catch(err){
  19. res.writeHead(404, {'Content-Type': 'text/html'});
  20. res.end();
  21. return false;
  22. }
  23. res.writeHead(200, { "Content-Type": MIME.lookup(ext)+';charset=utf-8'});
  24. res.end(data,'utf-8');
  25. //console.log(query)
  26. }else{
  27. try{
  28. data = FILE.readFileSync(assetFile,'binary');
  29. res.writeHead(200, { "Content-Type": MIME.lookup(ext),"Access-Control-Allow-Origin":"*"});
  30. res.end(data,'binary');
  31. }catch(err){
  32. res.writeHead(404, {'Content-Type': 'text/html'});
  33. res.end();
  34. }
  35. }
  36. }else{
  37. res.writeHead(404, {'Content-Type': 'text/html'});
  38. res.end();
  39. }
  40. }
  41. }
  42. module.exports=Static;