I have logs files in server directory i wanted to display file names to client side so i have created readDirectory.js
that is reading names correctly Now i am very new to node.js
and i am trying to send json
data to client but its not happening, How can i send log files name to client using express ?
我在服务器目录中记录文件我想向客户端显示文件名,所以我创建了正确读取名称的readDirectory.js现在我对node.js很新,我试图将json数据发送到客户端但是它不是发生了,如何使用快递将日志文件名称发送给客户端?
readDirectory.js
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
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
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;
});
}
}
});
2 个解决方案
#1
1
your readDirectory.js should look like this:
你的readDirectory.js应如下所示:
var fs = require('fs');
var path = './Logs'
var Logs = [];
function readDirectory(callback){
fs.readdir(path, function(err, items) {
Logs.push(items);
callback(Logs);
});
}
exports.readDirectory = readDirectory;
and your app.js should be like this:
你的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){
readDirectory.readDirectory(function(logFiles){
res.json({files : logFiles});
});
});
Hope this Help !
希望这个帮助!
#2
2
You have to put serialize the Logs
array into json and send it back to client
您必须将Logs数组序列化为json并将其发送回客户端
app.get('/logs',function(req,res){
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(readDirectory.readDirectory()));
});
Or
要么
app.get('/logs',function(req,res){
res.json(readDirectory.readDirectory());
});
#1
1
your readDirectory.js should look like this:
你的readDirectory.js应如下所示:
var fs = require('fs');
var path = './Logs'
var Logs = [];
function readDirectory(callback){
fs.readdir(path, function(err, items) {
Logs.push(items);
callback(Logs);
});
}
exports.readDirectory = readDirectory;
and your app.js should be like this:
你的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){
readDirectory.readDirectory(function(logFiles){
res.json({files : logFiles});
});
});
Hope this Help !
希望这个帮助!
#2
2
You have to put serialize the Logs
array into json and send it back to client
您必须将Logs数组序列化为json并将其发送回客户端
app.get('/logs',function(req,res){
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(readDirectory.readDirectory()));
});
Or
要么
app.get('/logs',function(req,res){
res.json(readDirectory.readDirectory());
});