node.js实际应用【模拟Apache服务器】【express】

  1. js文件
// nodemon .\src\apache_express.js

var path = require('path')
var express = require('express')
var fs = require('fs');
// var url = require('url');
var multer = require('multer');

// 文件名称后缀会丢失
// var upload = multer({dest:'upload/'})
// 解决文件名称后缀会丢失
var storage = multer.diskStorage({
  //设置上传后文件路径,uploads文件夹需要手动创建!!!
     destination: function (req, file, cb) {
         cb(null, './public/upload')
    }, 
  //给上传文件重命名,获取添加后缀名
   filename: function (req, file, cb) {
      //  var fileFormat = (file.originalname).split(".");
      //  cb(null, file.fieldname + '-' + Date.now() + "." + fileFormat[fileFormat.length - 1]);
      cb(null, file.originalname);
   }
});  
//添加配置文件到muler对象。
var upload = multer({
    storage: storage
});


var app = express()
const port = 3000

// view engine setup
app.engine('art', require('express-art-template'));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'art');

//static
app.use(express.static('./public/'))

// function
var getFiles =  function(res,url,callback){
  // 异步读取
  //文件夹处理
  if(!isFile(url)){
    fs.readdir(url, function(error, files){
      if (error) {
        return console.log(error) 
      }
      var dirList = [];
      var fileList =[];
  
      files.forEach(function(iterm){
        var fullPath =  path.join(url,iterm);
        // 同步读取
        var stat = fs.lstatSync(fullPath);
        if (stat.isFile()){
          fileList.push({'fullPath':fullPath,'fileSise':stat.size,'mtime':stat.mtime});
        }else{
          // console.log('iterm:',iterm)
          // console.log('fullPath:',fullPath)
          dirList.push(fullPath);
        }
      });
  
    // readdir是异步的,但是这里和forEach是同步的,也就是forEach执行完才执行这里
    // console.log('dir: ',dirList)
    // console.log('file: ',fileList)
    obj = {
      title: url,
      files: files,
      dirList:dirList,
      fileList:fileList,}
  
    callback(res,obj)
    });
    // 下面的打印会报参数未定义的错误,因为readdir异步还没执行
    // console.log('dir: ',dirList)
    // console.log('file: ',fileList)
  // 文件处理
  }else{
    fs.readFile(url, function(error, data){
      if (error) {
        return response.end('404 Not Found');
      }
      var fileEnd = path.extname(url);
      if (fileEnd === '.PNG') {
        res.setHeader('Content-Type', 'image/jpeg');
      }
      if (fileEnd === '.txt') {
        res.setHeader('Content-Type', 'text/plain; charset=utf-8');
      }
      res.send(data)
    });
  }
}


//返回渲染后页面
var render_res = function(res,obj){
  res.render('index.art',obj);
} 


//判断url对应的是否是文件
var isFile = function(url){
  var fileEnd = path.extname(url);
  return fileEnd !== ''
}

// routes
app.get('/', function (req, res) {
  getFiles(res,'.',render_res)
});


// 只能匹配一级目录
// app.get('/:url', function (req, res) {
//     var url = req.params.url
//     console.log(url)
//     getFiles(res,url,render_res)
// });


// 匹配所有目录
app.get('/*', function (req, res) {
  var url = '.'+req.url
  // console.log(url)
  getFiles(res,url,render_res)
});


// 上传
app.post('/upload', upload.single('myfile'), function(req, res, next){
  //console.log(req);
  res.send('upload OK!');
});


app.listen(port, () => {
// app.listen(port, function()  {
  console.log(`Example app listening at http://localhost:${port}`)
})



  1. index.art

<html dir="ltr" lang="zh" i18n-processed="">

<head>
  <meta charset="utf-8">
  <meta name="google" value="notranslate">
  
  <style>
    h1 {
      border-bottom: 1px solid #c0c0c0;
      margin-bottom: 10px;
      padding-bottom: 10px;
      white-space: nowrap;
    }

    table {
      border-collapse: collapse;
    }

    th {
      cursor: pointer;
    }

    td.detailsColumn {
      -webkit-padding-start: 2em;
      text-align: end;
      white-space: nowrap;
    }

    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;
    }

    html[dir=rtl] a {
      background-position-x: right;
    }

    #parentDirLinkBox {
      margin-bottom: 10px;
      padding-bottom: 10px;
    }

    #listingParsingErrorBox {
      border: 1px solid black;
      background: #fae691;
      padding: 10px;
      display: none;
    }
  </style>
  <title id="title">{{ title }}</title>
</head>

<body>
  <h1 id="header">索引</h1>
  <div id="parentDirLinkBox">
    <a id="parentDirLink" class="icon up" href="..">
    <span id="parentDirText">[上级目录]</span>
  </a>
  </div>
  <table>
    <thead>
      <tr class="header" id="theader">
        <th onclick="javascript:sortTable(0);">名称</th>
        <th class="detailsColumn" onclick="javascript:sortTable(1);">
          大小
        </th>
        <th class="detailsColumn" onclick="javascript:sortTable(2);">
          修改日期
        </th>
      </tr>
    </thead>
    <tbody id="tbody">
      <!-- file list -->
      {{each fileList}}
      <tr>
        <td data-value="{{ $value.fullPath }}"><a class="icon file" draggable="true"  href="/{{ $value.fullPath }}" >{{ $value['fullPath'] }}</a></td>
        <td class="detailsColumn" data-value="0">{{ $value.fileSise }}</td>
        <td class="detailsColumn" data-value="{{ $value.mtime }}">{{ $value.mtime }}</td>
      </tr>
      {{/each}}
      <!-- dir list -->
      {{each dirList}}
      <tr>
        <td data-value="{{ $value }}"><a class="icon dir" href="/{{ $value }}" >{{$value}}/</a></td>
        <td class="detailsColumn" data-value="0"></td>
        <td class="detailsColumn" data-value="1509589967">2017/11/2 上午10:32:47</td>
      </tr>
      {{/each}}
    </tbody>
  </table>
  <!-- upload -->
  <hr />
  <form action="/upload" method="post" enctype="multipart/form-data">
            <input type="file" name="myfile" id="myfile" value="" />
            <input type="submit" value="上传"/>
  </form>

</body>
</html>



posted @ 2021-06-19 23:03  该显示昵称已被使用了  阅读(57)  评论(0编辑  收藏  举报