这篇文章将为大家详细讲解有关使用Node怎么实现一个Axure文件在线预览功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
实现代码如下
const Koa = require('koa'); const path = require('path'); const fs = require('fs'); let mimes = { 'css': 'text/css', 'html': 'text/html', 'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'json': 'application/json', 'js': 'text/javascript', 'xml': 'text/xml', 'png': 'image/png', 'pdf': 'application/pdf', 'less': 'text/css', 'gif': 'image/gif', 'txt': 'text/plain', 'tiff': 'image/tiff', 'svg': 'image/svg+xml' }; // Scan dir function walk(reqPath) { let files = fs.readdirSync(reqPath); let fileList = []; for (let i = 0, len = files.length; i< len; i++) { let item = files[i]; if (item.startsWith('.')) { continue; } let realPath = reqPath + '/' + files[i]; if (isDir(realPath)) { fileList.push(filter(reqPath, files[i])); } else { fileList.push(files[i]); } } return fileList; } function isDir(path){ return fs.existsSync(path) && fs.statSync(path).isDirectory(); } // function filter(reqPath, filePath) { let files = fs.readdirSync(reqPath + '/' + filePath); for (let i = 0; i < files.length; i++) { let item = files[i]; if (item.endsWith('index.html')) { return filePath + '/' + 'index.html'; } } return filePath; } function dir(url, reqPath) { let contentList = walk(reqPath); let result = '<div align="center" >Product Document</div><ul>'; if (url == '/') { url = ''; } for (let [index, item] of contentList.entries()) { let realHref = url + '/' + item; let realItem = item.split('/'); result += '<li> <a href=' + realHref + '>' + realItem[0] + '</a></li>'; } result = result + '</ul>'; return result; } async function file (url, filePath) { let resultMime = parseMime(url); let content; if (resultMime && resultMime.indexOf('image/') >= 0) { content = fs.readFileSync(filePath, 'binary'); } else { content = fs.readFileSync(filePath, 'utf8'); } return content; } async function content(ctx, fullStaticPath) { let url = decodeURI(ctx.url); let reqPath = path.join(fullStaticPath, url); let exist = fs.existsSync(reqPath); let content = ''; if (!exist) { content = 'Local file not exists'; } else { let stat = fs.statSync(reqPath); if (stat.isDirectory()) { content = dir(ctx.url, reqPath); } else { content = await file(ctx.url, reqPath); } } return content; } function parseMime(url) { let extName = path.extname(url); extName = extName ? extName.slice(1) : 'unknown'; return mimes[extName]; } const app = new Koa(); const staticPath = '../onlinedoc';`请输入代码` app.use(async(ctx) => { let fullStaticPath = path.join(__dirname, staticPath); let resultContent = await content(ctx, fullStaticPath); let resultMime = parseMime(ctx.url); if (resultMime) { ctx.type = resultMime; } if(resultMime && resultMime.indexOf('image/') >= 0) { ctx.res.writeHead(200); ctx.res.write(resultContent, 'binary'); ctx.res.end(); } else { ctx.body = resultContent; } }); app.listen(8080, () => { console.log('Running'); });
关于使用Node怎么实现一个Axure文件在线预览功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。