Array is duplicated on every refresh using ExpressJS and NodeJS - javascript

var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require("path");
const readline = require('readline');
const directoryPath = path.resolve(__dirname,"../logtrace-filestore/edepoze-logs");
var logtraceArr = [];
router.get("/",function(req,res,next){
const fetchFileContentParsed = async (fileContentObj) => {
var logtraceObj = {
Direction : '',
FromServer: '',
ToServer:''
};
var pipearray = fileContentObj.toString().split("|");
for( i=0;i<pipearray.length;i++){
var Direction = new Array();
Direction = pipearray[5].split("::");
if (Direction.length == 2) {
logtraceObj.Direction = Direction[1];
} else {
logtraceObj.Direction = "";
}
}
logtraceArr.push(logtraceObj);
return {
logtraceArr
}
}
// Iterates all users and returns their Github info.
const getfileContent = async (files) => {
const fileContent = files.map((file) => {
const filePath = path.join(directoryPath,file);
//console.log("filePath ----->",filePath);
const readStream = fs.createReadStream(filePath);
const fileContent = readline.createInterface({
input: readStream
});
fileContent.on('line', function(line) {
const regExp = /\[Event([^]+\])/g
var matchedContent;
if ((matchedContent = line.match(regExp)) != null){
return fetchFileContentParsed(matchedContent) // Async function that fetches the user info.
.then((a) => {return a })
}
})
})
return Promise.all(fileContent) // Waiting for all the requests to get resolved.
}
function readFiles(dirname) {
//console.log(dirname);
fs.readdir(dirname, async function (err,filenames)
getfileContent(filenames).then((a) => {return a })
});
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
//console.log(readFiles(directoryPath))
readFiles(directoryPath,logtraceArr);
res.send(logtraceArr);
});
module.exports = router;
I have been trying to send the var logtraceArr = []; value to the browser using Expressjs but one first call empty array is displayed on 2nd call array is filled with first called function and on 3rd call array is appended with the same element.
I want the content to be displayed on the first.

You define logtraceArr in the global scope, so it never will be cleared. If you want to have an empty array for each request - define logtraceArr inside the router callback (for example on the line before calling readFiles)
Promise.all() doesn't wait if you don't await it. To wait you can return result of getfileContent from readFiles and then send request after result of readFiles. Fof example:
async function readFiles(dirname) {
fs.readdir(dirname, async function (err,filenames)
await getfileContent(filenames).then((a) => {return a })
});
return
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
readFiles(directoryPath,logtraceArr).then(() => res.send(logtraceArr));

Related

Array is duplicated on every refresh using ExpressJS and NodeJS

I have been trying to send the var logtraceArr = []; value to the browser using Expressjs but one first call empty array is displayed on 2nd call array is filled with first called function and on 3rd call array is appended with the same element.
I want the content to be displayed on the first.
var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require("path");
const readline = require('readline');
const directoryPath = path.resolve(__dirname,"../logtrace-filestore/edepoze-logs");
var logtraceArr = [];
router.get("/",function(req,res,next){
const fetchFileContentParsed = async (fileContentObj) => {
var logtraceObj = {
Direction : '',
FromServer: '',
ToServer:''
};
var pipearray = fileContentObj.toString().split("|");
for( i=0;i<pipearray.length;i++){
var Direction = new Array();
Direction = pipearray[5].split("::");
if (Direction.length == 2) {
logtraceObj.Direction = Direction[1];
} else {
logtraceObj.Direction = "";
}
}
logtraceArr.push(logtraceObj);
return {
logtraceArr
}
}
// Iterates all users and returns their Github info.
const getfileContent = async (files) => {
const fileContent = files.map((file) => {
const filePath = path.join(directoryPath,file);
//console.log("filePath ----->",filePath);
const readStream = fs.createReadStream(filePath);
const fileContent = readline.createInterface({
input: readStream
});
fileContent.on('line', function(line) {
const regExp = /\[Event([^]+\])/g
var matchedContent;
if ((matchedContent = line.match(regExp)) != null){
return fetchFileContentParsed(matchedContent) // Async function that fetches the user info.
.then((a) => {return a })
}
})
})
return Promise.all(fileContent) // Waiting for all the requests to get resolved.
}
function readFiles(dirname) {
//console.log(dirname);
fs.readdir(dirname, async function (err,filenames)
getfileContent(filenames).then((a) => {return a })
});
}
res.header("Access-Control-Allow-Origin", "*");
res.contentType('application/json');
//console.log(readFiles(directoryPath))
readFiles(directoryPath,logtraceArr);
res.send(logtraceArr);
});
module.exports = router;
i want the content to be loaded and print the array only once on refresh of the page

How to create an ipfs hash from an image source?

I have the src (data) of an image, that is not saved yet and doesn't have a path yet.
I would like to know the future ipfs hash that will result from it once it is saved and sent to ipfs.
So far I have done this, but the hashes don't match.
import { saveAs } from 'file-saver';
const dagPB = require('ipld-dag-pb')
const UnixFS = require('ipfs-unixfs')
func = async () => {
let bgImage = await import(`./images/bg.png`);
let bodyImage = await import(`./images/body.png`);
let headImage = await import(`./images/head.png`);
let eyesImage = await import(`./images/eye.png`);
let mouthImage = await import(`./images/mouth.png`);
let levelImage = await import(`./images/level.png`);
src = await mergeImages([
bgImage.default,
bodyImage.default,
headImage.default,
eyesImage.default,
mouthImage.default,
levelImage.default,
]);
image.src = src;
saveAs(image.src, `photo.png`);
const fileBuffer = Buffer.from(image.src)
const file = new UnixFS('file', fileBuffer)
dagPB.DAGNode.create(file.marshal(), (err, node) => {
if(err) return console.error(err)
console.log(node._cid.toBaseEncodedString())
})
}
What is missing or wrong ?
And here is what I did.
const Hash = require('ipfs-only-hash')
func = async () => {
let bgImage = await import(`./images/bg${ex}.png`);
let bodyImage = await import(`./images/body${ex}.png`);
let headImage = await import(`./images/head${ex}.png`);
let eyesImage = await import(`./images/eye${ex}.png`);
let mouthImage = await import(`./images/mouth${ex}.png`);
let levelImage = await import(`./images/level${ex}.png`);
src = await mergeImages([
bgImage.default,
bodyImage.default,
headImage.default,
eyesImage.default,
mouthImage.default,
levelImage.default,
]);
const endOfPrefix = src.indexOf(",");
const cleanStrData = src.slice(endOfPrefix+1);
const imageData = Buffer.from(cleanStrData, "base64");
const imageHash = await Hash.of(imageData);
console.log("fetch data CID: " + imageHash)
}
Remove the header of the data, to keep only the data and then hash it it with ipfs-hash-only.
The image hash is then written in the .json and the same process is used to hash the .json and know before hand the metadata's ipfs address.

Json response working in one function. and not the next?

I have put the code below my for a simple weather app using Open Weather
The code to get the icon code from the JSON works in the first block but not in the second?
i can console log the variable no problem but when i am trying to display the icon it isnt loading the png file?
The line in question is
let icon1 = jsonResponse.list[8].weather[0].icon
nextDayWeather.src = `/resources/icons/${icon1}.png`
full code for reference including same code working in an earlier function
async function getWeather(input) {
const city = input
try {
const response = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=`)
if (response.ok) {
const jsonResponse = await response.json()
const {icon} = jsonResponse.weather[0]
currTemp.innerText = Math.floor(jsonResponse.main.temp - 273)
currWind.innerText = jsonResponse.wind.speed
currWeather.src = `/resources/icons/${icon}.png`
return
}
throw new Error('Request Failed')
} catch(error) {
window.location = '/'
}
}
async function extraWeather(input) {
const city = input
try {
const response = await fetch(`http://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=`)
if (response.ok){
const jsonResponse = await response.json()
let icon1 = jsonResponse.list[8].weather[0].icon
let icon2 = jsonResponse.list[16].weather[0].icon
let icon3 = jsonResponse.list[24].weather[0].icon
nextDayWeather.src = `/resources/icons/${icon1}.png` //needs fixing??
nextDayWind.innerText = jsonResponse.list[8].wind.speed
nextDayTemp.innerText = Math.floor(jsonResponse.list[8].main.temp - 273)
nextDayWeather2.src = `/resources/icons/${icon2}.png`
nextDayWind2.innerText = jsonResponse.list[16].wind.speed
nextDayTemp2.innerText = Math.floor(jsonResponse.list[16].main.temp - 273)
nextDayWeather3.src = `/resources/icons/${icon3}.png`
nextDayWind3.innerText = jsonResponse.list[24].wind.speed
nextDayTemp3.innerText = Math.floor(jsonResponse.list[24].main.temp - 273)
console.log(jsonResponse.list[16].weather[0].icon)
return
}
throw new Error ('error') }
catch(error){
console.log(error)
}
}

Why is the module export variable empty?

I'm new to nodejs.
Here is my .js file. I'm trying to expose audioData variable to other functions. audioData variable value is being empty outside the function. I see the value when I print inside the function. What could be wrong?
'use strict';
var asyncrequest = require('request');
var xml2js = require('xml2js');
var parseString = xml2js.parseString;
var audioData = [];
asyncrequest("http://example.com/feed", function(error, responsemeta, body) {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url;
audioData.push(singleObj);
});
});
console.dir(audioData);
});
module.exports = audioData;
console.log("Program ended");
You'll have to return a promise for the audioData, not the audioData itself! You can learn more about promises elsewhere. Happily there's a promisified version of request, request-promise, that you can use like so:
'use strict';
var rp = require('request-promise');
var xml2js = require('xml2js');
var parseString = xml2js.parseString;
var audioData = [];
var promiseForAudioData = rp('http://example.com/feed')
.then(body => {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url;
audioData.push(singleObj);
});
});
return audioData;
})
.catch(console.error.bind(console));
module.exports = promiseForAudioData;
console.log("Program ended");
If you don't want to use promises, you can either export inside the callback or export the request method itself.
asyncrequest("http://example.com/feed", function(error, responsemeta, body) {
parseString(body, function(err, result){
var stories = result['rss']['channel'][0]['item'];
console.log("Total stories: " + stories.length);
stories.forEach(function(entry) {
var singleObj = {}
singleObj['title'] = entry['title'][0];
singleObj['value'] = entry['enclosure'][0].$.url; audioData.push(singleObj);
});
module.exports = audioData;
});
});
// Or
exports.get = function (callback) {
return asyncrequest(/* ... */, callback);
}
// Other module
require("./module").get(function (audioData) {
/* Do something */
})

Callback is not a function

I am trying to retrieve a json object to use it in another module, but I have a problem with callback. I have the error "callback is not a function". I use callback because my variable description is undefined, so i guess it's a problem of asynchronous.
Could you help me plz :)
var leboncoin = function () {
var http = require('http')
var bl = require('bl')
http.get("http://www.website.com", function (response, callback) {
response.pipe(bl(function (err, data) {
if (err) {
return console.error(err)
callback(err);
}
var data = data.toString()
var brand = ...
var model = ...
var releaseDate = ...
var km = ...
var fuel = ...
var gearbox = ...
description.Brand = brand;
description.Model = model;
description.Year = releaseDate;
description.KM = km;
description.Fuel = fuel;
description.Gearbox = gearbox;
callback(description);
return (description)
/*console.log(description.Brand);
console.log(description.Model);
console.log(description.Year);
console.log(description.KM);
console.log(description.Fuel);
console.log(description.Gearbox);*/
}))
})
}
exports.leboncoin = leboncoin;
var module = require('./leboncoin');
var res = module.leboncoin();
console.log(res);
Callbacks aren't magic that just appear. You need to define a parameter to your function and pass the callback you want to use.
// --------------------------v
var leboncoin = function (callback) {
var http = require('http')
var bl = require('bl')
http.get("http://www.website.com", function (response) {
response.pipe(bl(function (err, data) {
if (err) {
callback(err);
return;
}
var data = data.toString()
var description = { /* your description object */ }
callback(description);
}))
})
}
exports.leboncoin = leboncoin;
var module = require('./leboncoin');
// -----------------vvvvvvvv
module.leboncoin(function(res) {
console.log(res);
});
The method http.get requires a function that accepts only a parameter named response (or whatever you want, the name doesn't matter indeed), thus your second one, callback, is undefined and invoking it will end ever in that error.

Categories