我有服務器目錄中的日誌文件我想顯示文件名稱到客戶端,所以我創建了readDirectory.js
,正在讀取名稱現在我很新node.js
,我試圖發送json
數據給客戶端,但它沒有發生,如何使用快遞將日誌文件名稱發送給客戶端?如何從目錄中讀取文件並將其作爲JSON發送給客戶端?
readDirectory.js
var fs = require('fs');
var path = './Logs'
var Logs = [];
function readDirectory(){
fs.readdir(path, function(err, items) {
Logs.push(items);
/* console.log(items);
for (var i=0; i<items.length; i++) {
console.log(items[i]);
}*/
});
return Logs;
}
exports.readDirectory = readDirectory;
app.js
var express = require('express');
var app = express();
var readDirectory = require('./readDirectory');
app.use(express.static(__dirname + "/public"));
app.get('/logs',function(req,res){
res.send(readDirectory.readDirectory());
});
angularFactory.js
angular.module('App').factory('DitFactory', function ($http) {
'use strict';
var data;
return {
data:"data from factory"
getLogs: function() {
return $http.get('/logs')
.then(function (response) {
return response.data;
});
}
}
});
感謝它的工作,我感謝你的幫助! – hussain