您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。