实际上我在index.js文件中有一个路径名.每当我选择目录时,我都可以在node.js控制台屏幕中看到当前目录名.
现在,我想在template.html页面的文本框中显示该目录名称.有人可以帮我解决这个问题吗?
在index.js中,在这一部分我能够在控制台中看到路径名:
app.get('/files',function(req,res) {
var currentDir = dir;
var query = req.query.path || '';
if (query) currentDir = path.join(dir,query);
console.log("browsing ",currentDir);
我的index.js:
#!/usr/bin/env node
var http = require('http');
var _ = require('lodash');
var express = require('express');
var fs = require('fs');
var path = require('path');
var util = require('util');
var program = require('commander');
function collect(val,memo) {
if(val && val.indexOf('.') != 0) val = "." + val;
memo.push(val);
return memo;
}
program
.option('-p,--port ','Port to run the file-browser. Default value is 8088')
.option('-e,--exclude ','File extensions to exclude. To exclude multiple extension pass -e multiple times. e.g. ( -e .js -e .cs -e .swp) ',collect,[])
.parse(process.argv);
var app = express();
var dir = process.cwd();
app.use(express.static(dir)); //app public directory
app.use(express.static(__dirname)); //module directory
var server = http.createServer(app);
if(!program.port) program.port = 8088;
server.listen(program.port);
console.log("Please open the link in your browser http://:" + program.port);
app.get('/files',currentDir);
fs.readdir(currentDir,function (err,files) {
if (err) {
throw err;
}
var data = [];
files
.filter(function (file) {
return true;
}).forEach(function (file) {
try {
//console.log("processing ",file);
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,file) });
}
} catch(e) {
console.log(e);
}
});
data = _.sortBy(data,function(f) { return f.Name });
res.json(data);
});
});
app.get('/',res) {
res.redirect('lib/template.html');
});
我的template.html:
File BrowserBrowse
Up
$(document).ready(function(){
$("#butDiv").click(function(){
$("#showDiv").toggle();
});
});
我的app.js:
(function($){
var extensionsMap = {
".zip" : "fa-file-archive-o",".gz" : "fa-file-archive-o",".bz2" : "fa-file-archive-o",".xz" : "fa-file-archive-o",".rar" : "fa-file-archive-o",".tar" : "fa-file-archive-o",".tgz" : "fa-file-archive-o",".tbz2" : "fa-file-archive-o",".z" : "fa-file-archive-o",".7z" : "fa-file-archive-o",".mp3" : "fa-file-audio-o",".cs" : "fa-file-code-o",".c++" : "fa-file-code-o",".cpp" : "fa-file-code-o",".js" : "fa-file-code-o",".xls" : "fa-file-excel-o",".xlsx" : "fa-file-excel-o",".png" : "fa-file-image-o",".jpg" : "fa-file-image-o",".jpeg" : "fa-file-image-o",".gif" : "fa-file-image-o",".mpeg" : "fa-file-movie-o",".pdf" : "fa-file-pdf-o",".ppt" : "fa-file-powerpoint-o",".pptx" : "fa-file-powerpoint-o",".txt" : "fa-file-text-o",".log" : "fa-file-text-o",".doc" : "fa-file-word-o",".docx" : "fa-file-word-o",};
function getFileIcon(ext) {
return ( ext && extensionsMap[ext.toLowerCase()]) || 'fa-file-o';
}
var currentPath = null;
var options = {
"bProcessing": true,"bServerSide": false,"bPaginate": false,"bAutoWidth": false,"sScrollY":"250px","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 " " + data.Name +"";
} else {
return " " + data.Name +"";
}
}
}
]
};
var table = $(".linksholder").dataTable(options);
console.log(table);
$.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;
});
});
})(jQuery);
当前浏览格式:
必需的浏览格式: