123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- /**
- * @Author HonorLee (dev@honorlee.me)
- * @Version 1.0 (2018-05-04)
- * @License MIT
- */
- 'use strict'
- //TODO Rewrite
- var Static = {
- load:function(assetFile,req,res){
- let ext = Path.extname(assetFile);
- if(!ext||ext==''){
- res.writeHead(404, {'Content-Type': 'text/html'});
- res.end();
- return;
- }
- if(ext.match(/png|jpg|jpeg|gif|ico/)){
- IMAGE.load(URL.parse(assetFile,true).pathname,ext,req,res);
- }else if(FILE.existsSync(assetFile)){
- var data;
- if(ext.match(/txt|js|css|html|json/)){
- try{
- data = FILE.readFileSync(assetFile,'utf-8');
- }catch(err){
- res.writeHead(404, {'Content-Type': 'text/html'});
- res.end();
- return false;
- }
- res.writeHead(200, { "Content-Type": MIME.lookup(ext)+';charset=utf-8'});
- res.end(data,'utf-8');
- //console.log(query)
- }else{
- try{
- data = FILE.readFileSync(assetFile,'binary');
- res.writeHead(200, { "Content-Type": MIME.lookup(ext),"Access-Control-Allow-Origin":"*"});
- res.end(data,'binary');
- }catch(err){
- res.writeHead(404, {'Content-Type': 'text/html'});
- res.end();
- }
- }
- }else{
- res.writeHead(404, {'Content-Type': 'text/html'});
- res.end();
- }
- }
- }
- module.exports=Static;
|