使用nodejs和art-template模板引擎实现apache的部分功能
升级一下上一篇的功能
首先还是配置:config.js
module.exports = { host: 'http://127.0.0.1', //主机名 port: 3000, //服务器端口号 documentRoot: 'E:/webdev', //根目录 directoryBrowse: true, //是否开启目录浏览功能 directoryIndex: [ //目录默认访问页 'index.html', 'index.htm', 'deflaut.html' ], charset: 'utf-8', mineType: { image: { gif: 'image/gif', jpeg: 'image/jpeg', jpg: 'image/jpeg', png: 'image/png', }, text: { css: 'text/css', htm: 'text/html', html: 'text/html', js: 'application/x-javascript', json: 'application/json', pdf: 'application/pdf', }, other: 'text/plain' } }
接着是服务器代码:index.js
const http = require('http') const fs = require('fs') const path = require('path') const tpl = require('art-template') const config = require('./config') const server = http.createServer() const documentRoot = config.documentRoot server.on('request', function (req, res) { let url = req.url console.log(url) let tmp = documentRoot + url let exist = fs.existsSync(tmp) if (exist) { let stats1 = fs.statSync(tmp) if (stats1.isDirectory()) { for (let key in config.directoryIndex) { let file = tmp + '/' + config.directoryIndex[key] console.log(`${config.host}:${config.port + url + config.directoryIndex[key]}`); if (fs.existsSync(file)) { res.writeHead(302, {'Location': `${config.host}:${config.port + url + config.directoryIndex[key]}`}) res.end() return } } if (!config.directoryBrowse) { //没有开放目录浏览权限 res.writeHead(403, {"Content-Type": "text/html"}); res.end('<h2>403 forbidden!!</h2>') return } fs.readFile('./template-apache.html', function (err, data) { if (err) { return res.end('404 Not Found.') } let prevDisplay = 'block' if (url == '/') { prevDisplay = 'none' } fs.readdir(tmp, function (err, files) { if (err) { return res.end('Can not find www dir.') } let fileObjArr = [] for(key in files){ fileObjArr[key] = {} fileObjArr[key].name = files[key] fileObjArr[key].type = 'file' fileObjArr[key].separate = '' let stats = fs.statSync(tmp + files[key]) if (stats.isDirectory()) { fileObjArr[key].type = 'dir' fileObjArr[key].separate = '/' } fileObjArr[key].href = `${config.host}:${config.port + url + files[key] + fileObjArr[key].separate}` } let parentPath = url.substring(0, url.substr(0, url.length - 1).lastIndexOf('/') + 1) let htmlStr = tpl.render(data.toString(), { title: 'Index Of' + url, files: fileObjArr, prevDisplay, parentPath }) res.end(htmlStr) }) }) } else { //渲染文件 fs.readFile(tmp, function (err, data) { if (err) { res.end() } let ext = path.extname(tmp).substring(1) if (ext in config.mineType.text) { res.setHeader('Content-Type', `${config.mineType.text[ext]}; charset=${config.charset}`) } else if (ext in config.mineType.image) { res.setHeader('Content-Type', `${config.mineType.text[ext]}`) } else { res.setHeader('Content-Type', `${config.mineType.other}; charset=${config.charset}`) } res.end(data) }) } } else { res.end() } }) server.listen(config.port, function () { console.log(`server running in: ${config.host}:${config.port}`) const cp = require('child_process') cp.exec(`start ${config.host}:${config.port}`) //自动打开浏览器 })
最后是模板:template-apache.html
<html> <head> <meta charset="utf-8"> <style> h1 { border-bottom: 1px solid #c0c0c0; margin-bottom: 10px; padding-bottom: 10px; white-space: nowrap; } table { border-collapse: collapse; } a.icon { -webkit-padding-start: 1.5em; text-decoration: none; } a.icon:hover { text-decoration: underline; } a.file { background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABnRSTlMAAAAAAABupgeRAAABHUlEQVR42o2RMW7DIBiF3498iHRJD5JKHurL+CRVBp+i2T16tTynF2gO0KSb5ZrBBl4HHDBuK/WXACH4eO9/CAAAbdvijzLGNE1TVZXfZuHg6XCAQESAZXbOKaXO57eiKG6ft9PrKQIkCQqFoIiQFBGlFIB5nvM8t9aOX2Nd18oDzjnPgCDpn/BH4zh2XZdlWVmWiUK4IgCBoFMUz9eP6zRN75cLgEQhcmTQIbl72O0f9865qLAAsURAAgKBJKEtgLXWvyjLuFsThCSstb8rBCaAQhDYWgIZ7myM+TUBjDHrHlZcbMYYk34cN0YSLcgS+wL0fe9TXDMbY33fR2AYBvyQ8L0Gk8MwREBrTfKe4TpTzwhArXWi8HI84h/1DfwI5mhxJamFAAAAAElFTkSuQmCC ") left top no-repeat; } a.dir { background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAd5JREFUeNqMU79rFUEQ/vbuodFEEkzAImBpkUabFP4ldpaJhZXYm/RiZWsv/hkWFglBUyTIgyAIIfgIRjHv3r39MePM7N3LcbxAFvZ2b2bn22/mm3XMjF+HL3YW7q28YSIw8mBKoBihhhgCsoORot9d3/ywg3YowMXwNde/PzGnk2vn6PitrT+/PGeNaecg4+qNY3D43vy16A5wDDd4Aqg/ngmrjl/GoN0U5V1QquHQG3q+TPDVhVwyBffcmQGJmSVfyZk7R3SngI4JKfwDJ2+05zIg8gbiereTZRHhJ5KCMOwDFLjhoBTn2g0ghagfKeIYJDPFyibJVBtTREwq60SpYvh5++PpwatHsxSm9QRLSQpEVSd7/TYJUb49TX7gztpjjEffnoVw66+Ytovs14Yp7HaKmUXeX9rKUoMoLNW3srqI5fWn8JejrVkK0QcrkFLOgS39yoKUQe292WJ1guUHG8K2o8K00oO1BTvXoW4yasclUTgZYJY9aFNfAThX5CZRmczAV52oAPoupHhWRIUUAOoyUIlYVaAa/VbLbyiZUiyFbjQFNwiZQSGl4IDy9sO5Wrty0QLKhdZPxmgGcDo8ejn+c/6eiK9poz15Kw7Dr/vN/z6W7q++091/AQYA5mZ8GYJ9K0AAAAAASUVORK5CYII= ") left top no-repeat; } a.up { background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAmlJREFUeNpsU0toU0EUPfPysx/tTxuDH9SCWhUDooIbd7oRUUTMouqi2iIoCO6lceHWhegy4EJFinWjrlQUpVm0IIoFpVDEIthm0dpikpf3ZuZ6Z94nrXhhMjM3c8895977BBHB2PznK8WPtDgyWH5q77cPH8PpdXuhpQT4ifR9u5sfJb1bmw6VivahATDrxcRZ2njfoaMv+2j7mLDn93MPiNRMvGbL18L9IpF8h9/TN+EYkMffSiOXJ5+hkD+PdqcLpICWHOHc2CC+LEyA/K+cKQMnlQHJX8wqYG3MAJy88Wa4OLDvEqAEOpJd0LxHIMdHBziowSwVlF8D6QaicK01krw/JynwcKoEwZczewroTvZirlKJs5CqQ5CG8pb57FnJUA0LYCXMX5fibd+p8LWDDemcPZbzQyjvH+Ki1TlIciElA7ghwLKV4kRZstt2sANWRjYTAGzuP2hXZFpJ/GsxgGJ0ox1aoFWsDXyyxqCs26+ydmagFN/rRjymJ1898bzGzmQE0HCZpmk5A0RFIv8Pn0WYPsiu6t/Rsj6PauVTwffTSzGAGZhUG2F06hEc9ibS7OPMNp6ErYFlKavo7MkhmTqCxZ/jwzGA9Hx82H2BZSw1NTN9Gx8ycHkajU/7M+jInsDC7DiaEmo1bNl1AMr9ASFgqVu9MCTIzoGUimXVAnnaN0PdBBDCCYbEtMk6wkpQwIG0sn0PQIUF4GsTwLSIFKNqF6DVrQq+IWVrQDxAYQC/1SsYOI4pOxKZrfifiUSbDUisif7XlpGIPufXd/uvdvZm760M0no1FZcnrzUdjw7au3vu/BVgAFLXeuTxhTXVAAAAAElFTkSuQmCC ") left top no-repeat; } #parentDirLinkBox { margin-bottom: 10px; padding-bottom: 10px; } </style> <title id="title">{{ title }}</title> </head> <body> <h1 id="header">{{ title }}</h1> <div id="parentDirLinkBox" style="display:{{ prevDisplay }}"> <a id="parentDirLink" class="icon up" href="{{ parentPath }}"> <span id="parentDirText">[上级目录]</span> </a> </div> <table> {{each files}} <tr> <td data-value="apple/"><a class="icon {{$value.type}}" href="{{$value.href}}">{{$value.name}}{{ $value.separate}}</a></td> </tr> {{/each}} </table> </body> </html>
end^_^