使用节点怎么实现一个Axure文件在线预览功能

  介绍

这篇文章将为大家详细讲解有关使用节点怎么实现一个Axure文件在线预览功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

实现代码如下

const  Koa =,要求(& # 39;洋槐# 39;);   const  path =,要求(& # 39;path & # 39;);   const  fs =,要求(& # 39;fs # 39;);   let  mimes =, {   & # 39;才能css # 39;:, & # 39;文本/css # 39;   & # 39;才能html # 39;:, & # 39;文本/html # 39;   & # 39;才能jpg # 39;:, & # 39;图像/jpeg # 39;   & # 39;才能jpeg # 39;:, & # 39;图像/jpeg # 39;   & # 39;才能json # 39;:, & # 39;应用程序/json # 39;   & # 39;才能js # 39;:, & # 39;文本/javascript # 39;   & # 39;才能xml # 39;:, & # 39;文本/xml # 39;   & # 39;才能png # 39;:, & # 39;图像/png # 39;   & # 39;才能pdf # 39;:, & # 39;应用程序/pdf # 39;   & # 39;才能少# 39;:,& # 39;文本/css # 39;   & # 39;才能gif # 39;:, & # 39;图像/gif # 39;   & # 39;才能txt # 39;:, & # 39;文本/平原# 39;   & # 39;才能tiff # 39;:, & # 39;图像/tiff # 39;   & # 39;才能svg # 39;:, & # 39;图像/svg + xml # 39;   };//Scan  dir   function 步行(reqPath), {   let 才能;files =, fs.readdirSync (reqPath);   let 才能;fileList =, [];   for 才能;(let 小姐:=,0,,len =, files.length;, i<, len;,我+ +),{   ,,,let  item =,文件(我);   ,,,if  (item.startsWith(& # 39; # 39;公司)),{   ,,,,,继续;   ,,,}   ,,,let  realPath =, reqPath  +, & # 39;/& # 39;, +,文件(我);   ,,,if  (isDir (realPath)), {   ,,,,,fileList.push(过滤器(reqPath,文件[我]));   ,,,},{else    ,,,,,fileList.push(文件[我]);   ,,,}   ,,}   return 才能;文件列表;   }      function  isDir(路径){,   return 才能;fs.existsSync(路径),,,,fs.statSync(路径).isDirectory (),,   },//function 过滤器(reqPath, filePath), {   let 才能;files =, fs.readdirSync(时间+ reqPath  & # 39;/& # 39;, +, filePath);   for 才能;(let 小姐:=,0;,小姐:& lt;, files.length;,我+ +),{   ,,,let  item =,文件(我);   ,,,if  (item.endsWith (& # 39; index . html # 39;)), {   ,,,,,return  filePath  +, & # 39;/& # 39;, +, & # 39; index . html # 39;;   ,,,}   ,,}   return 才能;filePath;   }      function  dir (url, reqPath), {   let 才能;contentList =,走路(reqPath);   let 才能;result =, & # 39; & lt; div 对齐=癱enter",祝辞Product  Document
& lt; ul> & # 39;;   if 才能;(url ==, & # 39;/& # 39;), {   ,,,url =, & # 39; & # 39;;   ,,}   for 才能;(let [指数,项目],of  contentList.entries ()), {   ,,,let  realHref =, url  +, & # 39;/& # 39;, +,项目;   ,,,let  realItem =, item.split (& # 39;/& # 39;);   ,,,result  +=, & # 39; & lt; li>, & lt; a , href=https://www.yisu.com/zixun/' + realHref + '> ' + realItem[0] +” ';   }   结果=结果+“”;   返回结果;   }      异步函数文件(url, filePath) {   让resultMime=parseMime (url);   让内容;   如果(resultMime & & resultMime.indexOf(“图像/?>=0){   内容=fs。readFileSync (filePath“二进制”);   其他}{   内容=fs。readFileSync (filePath use utf8);   }   返回内容;   }      fullStaticPath异步函数内容(ctx) {   让url=decodeURI (ctx.url);   让reqPath=路径。加入(fullStaticPath、url);   让存在=fs.existsSync (reqPath);   让内容=";   如果存在(!){   内容=氨镜匚募淮嬖凇?   其他}{   让统计=fs.statSync (reqPath);   如果(stat.isDirectory ()) {=dir (ctx内容。url, reqPath);   其他}{   内容(ctx=等待文件。url, reqPath);   }   }   返回内容;   }      函数parseMime (url) {   让extName=path.extname (url);   extName=extName ?extName.slice(1):“未知”;   返回哑剧演员(extName);   }   const应用=new高雅();   const staticPath=? ./onlinedoc”;“请输入代码”   app.use(异步(ctx)=> {   让fullStaticPath=路径。加入(__dirname staticPath);   让resultContent=等待内容(ctx fullStaticPath);   让resultMime=parseMime (ctx.url);   如果(resultMime) {   ctx。类型=resultMime;   }   如果(resultMime & & resultMime.indexOf(“图像/?>=0){   ctx.res.writeHead (200);   ctx.res。写(resultContent“二进制”);   ctx.res.end ();   其他}{   ctx。身体=resultContent;   }   });   app.listen(8080年,()=> {   console.log(“奔跑”);   });

使用节点怎么实现一个Axure文件在线预览功能