admin管理员组文章数量:1406911
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;
});
}
}
});
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;
});
}
}
});
Share
edited Jun 29, 2016 at 18:07
hussain
asked Jun 29, 2016 at 15:33
hussainhussain
7,14121 gold badges87 silver badges165 bronze badges
3 Answers
Reset to default 2You 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());
});
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 !
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./a/56821924/8021938
Source: https://nodejs/api/fs.html#fs_fspromises_readfile_path_options
本文标签: javascriptHow can I read files from directory and send as JSON to clientStack Overflow
版权声明:本文标题:javascript - How can I read files from directory and send as JSON to client? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745055764a2639925.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论