NodeJs和JQuery:路径错误

时间:2016-06-22 04:09:15

标签: javascript jquery node.js express

我正在将文件浏览器构建到html页面中。此文件浏览器由JQuery和带有ExpressJs和DataTables的NodeJ组成。手头的问题是,当点击一个子目录目录时,它应该在点击的子目录中搜索和显示文件/目录;但是,它会搜索并显示原始目录的文件/目录。有问题的目录是[AppName] / Public / Assets } 这是html代码段:

 <body>
   <div class="panel panel-default mainpanel">
       <div class="panel-heading">
               Document Browser
               <span class="up">
                <i class="fa fa-level-up"></i> Back
               </span> 
        </div>
    <div class="panel-body">
          <table class="linksholder" id="linksholder">
          <thead>

          </thead>
          <tbody>
          <tr>
          <td>
          </td>
          </tr>

          </tbody>
          </table>
    </div>
    </div>
    <script>
    $(document).ready(function() {
var table = $(".linksholder").dataTable({
"fnCreatedRow": function(nRow, aData, iDataIndex) {
 if (!aData.IsDirectory) return;
 var path = aData.Path;
 $(nRow).bind("click", function(e) {
  $.get('/files?path=' + path).then(function(data) {
    table.fnClearTable();
    table.fnAddData(data);
    currentPath = path;
  });
  e.preventDefault();
 });
},
 "aoColumns": [
      { "sTitle": "", "mData": null, "bSortable": false, "sClass": "head0", "sWidth": "55px",
        "render": function (data, type, row, meta) {
          if (data.IsDirectory) {
            return "<a href='#' target='_blank'><i class='fa fa-folder'></i>&nbsp;" + data.Name +"</a>";
          } else {
            return "<a href='/" + data.Path + "' target='_blank'><i class='fa " +  "'></i>&nbsp;" + data.Name +"</a>";
          }
        }
      }
    ],
});
$.get('/files').then(function(data) {
table.fnClearTable();
table.fnAddData(data);
});
$(".up").bind("click", function(e){
if (!currentPath) return;
var idx = currentPath.lastIndexOf("/");
var path =currentPath.substr(0, idx);
$.get('/files?path='+ path).then(function(data){
    table.fnClearTable();
    table.fnAddData(data);
    currentPath = path;
 });
});

});



    </script>
    </body>

} 这是expressJs片段:

var express = require('express');
var routes = require('./routes');
var multer = require('multer');
var fs = require('fs');
var _ = require('lodash');
var path = require('path');
var app = express();
 app.get('/files', function(req, res) {
 var currentDir =  __dirname + '/public/assets/';
 var query = req.query.path || '';
 if (query) currentDir = path.join(currentDir, query);
 console.log("browsing ", currentDir);
 fs.readdir('/[AppName]/public/assets', function (err, files) {
 if (err) {
    throw err;
  }
  var data = [];
  files
  .forEach(function (file) {
    try {
            //console.log("processingile);
            var isDirectory = fs.statSync(path.join(currentDir,file)).isDirectory();
            if (isDirectory) {
              data.push({ Name : file, IsDirectory: true, Path : path.join(query, file)  });
            } else {
              var ext = path.extname(file);
              if(program.exclude && _.contains(program.exclude, ext)) {
                console.log("excluding file ", file);
                return;
              }       
              data.push({ Name : file, Ext : ext, IsDirectory: false, Path : path.join(query, file) });
            }

    } catch(e) {
      console.log(e); 
    }        

  });
  data = _.sortBy(data, function(f) { return f.Name });
  res.json(data);
 });
});

我知道这是在调用函数&#34; fnCreatedRow&#34;时调用的路径的问题,但我试图分析并且无法确定它。输出结果如下:

browsing  [AppName]/public/assets/:path
[ReferenceError: program is not defined]
 { [Error: ENOENT: no such file or directory, stat '[AppName]/public/assets/:path/1']
  errno: -2,
  code: 'ENOENT',
   syscall: 'stat',
   path: '[AppName]/public/assets/:path/1' }

这是目录[AppName]/public/assets中的文件,而不是[AppName]/public/assets/:path

0 个答案:

没有答案