7

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

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
  • 6,587
  • 18
  • 79
  • 152

3 Answers3

2

You have to put serialize the Logs array into json and send it back to client

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());
});
Huy Hoang Pham
  • 4,107
  • 1
  • 16
  • 28
  • still i do not see data i am not sure why – hussain Jun 29 '16 at 15:52
  • Try to use the web browser to access the JSON then? Or you can try `console.log(response)` – Huy Hoang Pham Jun 29 '16 at 15:55
  • i think first place i am not getting data from `readDirectory.js` to `app.js` for some reason i debuged it and i see 4 items in Logs array in `readDirectory` function but its not being passed to `app.js` – hussain Jun 29 '16 at 15:58
  • Did you debug the line `var readDirectory = require('./readDirectory');` in app.js – Huy Hoang Pham Jun 29 '16 at 16:03
  • `Server.connections property is deprecated. Use Server.getConnections method instead.` it is calling the function `readDirectory` but its not returning anything – hussain Jun 29 '16 at 16:11
1

your readDirectory.js should look like this:

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:

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 !

Lalit Goswami
  • 798
  • 1
  • 8
  • 21
0

Since Node v11.0.0 you can use require('fs').promises to get promises.

In your case you are pushing the array of the files in the directory into "Logs" array. Since in this case "Logs" will always be an array with only one value, I didn't use it.

You can do it like this:

var fs = require('fs').promises;
var path = './Logs'
async function readDirectory() {
    const files = await fs.readdir(path);
    return files;
}

In order to return the list of files in the folder change your app.js like this:

app.get('/logs', function (req, res) {
    readDirectory.readDirectory()
        .then((files) => {
            res.send(files);
        });
});

Link for the full answer: https://stackoverflow.com/a/56821924/8021938

Source: https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options

MasterPiece
  • 445
  • 7
  • 13