I have 2 json files which have static data.
File A.json
{
"data": ["A","B","C","D"]
}
File B.json
{
"data":["A","B","C"]
}
Now I want find the difference between these two files.
I tried using this code to get files by its name
const express = require("express");
const jsonfile = require("jsonfile");
const app = express();
const fs = require("fs");
app.use(express.json());
const BASEPATH = "./data"
app.get("/api/v1/data/:name", async (req,res) => {
const fileName = req.params.name;
const filePath = `${BASEPATH}/${fileName}.json`
try {
const readData = await jsonfile.readFile(filePath);
res.status(200).json(readData);
}catch (e) {
res.status(404).send(e);
}
});
The URL is : localhost:3000/api/v1/data/A
To add data I used,
app.put("/api/v1/data",(req,res) => {
const fileName = req.body.name;
const data = req.body.data;
const filePath = `${BASEPATH}/${fileName}.json`
fs.exists(filePath, async exists => {
if(exists) {
try {
await jsonfile.writeFile(filePath,{data:data});
res.status(200).send();
}catch(e) {
res.send(500).json(e);
}
} else {
try {
await jsonfile.writeFile(filePath,{data:data});
res.status(201).send();
}catch(e) {
res.send(500).json(e);
}
}
})
});
Example of added data:
{ "name":"C", "data":["A","B","Z"]}
URL is: localhost:3000/api/v1/data
you can try like this,
app.get("/api/v1/data/diff", async (req,res) => {
try {
const file1 = req.query.file1;
const file2 = req.query.file2;
console.log(file1,file2)
if(file1 === undefined || file2 === undefined ) {
res.status(401).send("BAD REQUEST. SEND FILE NAMES");
return;
} else {
const filePath1 = `${BASEPATH}/${file1}.json`
const filePath2 = `${BASEPATH}/${file2}.json`
const file1Data = await jsonfile.readFile(filePath1);
const file2Data = await jsonfile.readFile(filePath2);
let difference = file1Data.data.filter(x => !file2Data.data.includes(x));
let difference2 = file2Data.data.filter(x => !file1Data.data.includes(x));
res.status(200).json({diff: [...difference,...difference2]});
}
}catch(e) {
res.status(500).json(e);
}
});
you have to use the url like : localhost:3000/api/v1/data/diff?file1=A&file2=B
the output will be
{
"diff": [
"D"
]
}
Related
My problem is that i have to create Converter and this converter takes the XML files and converts it to XLSX and download it. i was able to do the first part where the XML convert to JSON, but i have difficult to do the rest of the code, now i am trying to take the file JSON and convert it to XLSX but my code is not working, i do not know why ?
ControllerXML2Json.js
const { json } = require('body-parser')
const fs = require('fs')
const xml2js = require('xml2js')
let jsonString
async function xml2json() {
try {
const xml = fs.readFileSync('src/data/dataset.xml')
const result = await xml2js.parseStringPromise(xml, { mergeAttrs: true })
jsonString = JSON.stringify(result, null, 4)
//console.log(json)
fs.writeFileSync('./public/data/result.json', jsonString)
//return jsonString
} catch (error) {
console.log(error)
}
}
module.exports = {
convert(req, res) {
let xmlFile = req.files.fileXML
xmlFile.mv('src/data/dataset.xml', function (err) {
if (err) {
return res.status(500).send(err)
} else {
xml2json()
}
})
res.redirect('/json')
}
}
ControllerJson2Xlsx.js
const { json } = require("body-parser");
const fs = require("fs");
const xlsx = require("json-as-xlsx");
const path = require('path');
const fileName = "data.xlsx";
async function generateExcelFromJSON(){
try {
let content = JSON.parse(fs.readFileSync("./data/result.json", "utf-8"));
let newWS = xlsx.utils.book_new();
let newWB = xlsx.utils.json_to_sheet(content);
xlsx.utils.book_append_sheet(newWB, newWS, "data");
await xlsx.writeFile("./public/data/data.xlsx", content);
// xlsx.writeFile(newWB, "./public/data/data.xlsx");
} catch (error) {
console.log(error);
}
}
module.exports = {
download(req, res) {
let jsonFile = req.files.fileJSON;
jsonFile.mv('./data/data.xlsx', function (err) {
if (err) {
return res.status(500).send(err);
} else {
xlsx();
}
});
res.redirect("/xlsx");
},
};
anyone know the reason of this problem? what is the issue?
in this code, i am trying to convert a blob into video format. but sometime it is working and sometime not. can anyone help me in this?
const recordVideo = require('../model/recordVideo_model')
const { join, dirname } = require('path');
const { fileURLToPath } = require('url')
const { mkdir, open, unlink, writeFile } = require('fs/promises');
const {Blob, Buffer} = require('buffer');
const { path } = require('#ffmpeg-installer/ffmpeg');
const ffmpeg = require('fluent-ffmpeg');
ffmpeg.setFfmpegPath(path)
// const __dirname = dirname(fileURLToPath(import.meta.url));
const saveData = async(data, roomid, course_id)=>{
//console.log(data);
const videoPath = join(__dirname, '../video');
//final folder name
//const dirName = new Date().toLocaleDateString().replace(/\./g, '_');
const dirName = roomid;
// console.log(`dirName: ${dirName}`);
//const dirPath = `${videoPath}/${dirName}`;
const dirPath = `./public/video/canvideo/${dirName}`;
const fileName= `${Date.now()}-${roomid}.webm`;
//const fileName= `${roomid}.webm`;
const tempFilePath = `${dirPath}/temp-${fileName}`;
const finalFilePath = `${dirPath}/${fileName}`;
let fileHandle;
try {
fileHandle = await open(dirPath);
console.log(`open the file${fileHandle}`)
}
catch {
await mkdir(dirPath);
console.log(`making directory${fileHandle}`)
}
finally {
if (fileHandle) {
fileHandle.close();
console.log(`closing ${fileHandle}`)
}
}
try {
const videoBlob = new Blob(data, {
type: 'video/webm'
})
const videoBuffer = Buffer.from(await videoBlob.arrayBuffer())
const res = await recordVideo.findOne({roomid:roomid, recordType:'canvas'})
if(!res){
await writeFile(tempFilePath, videoBuffer)
await ffmpeg(tempFilePath)
.outputOptions([
'-c:v libvpx-vp9',
'-c:a copy',
'-crf 35',
'-b:v 0',
'-vf scale=1280:720','-max_muxing_queue_size 1024'
])
.on('end', async () => {
await unlink(tempFilePath)
console.log(`*** File ${fileName} created`)
//insert database entry (recorded video entry with same details roomid, finename, finalfilepath, created at)
await recordVideo.create({roomid:roomid,filename:fileName,filePath:finalFilePath,recordType:'canvas', courseid:course_id});
}).on('error', function(err) {
console.log('An error occurred: ' + err.message);
})
.save(finalFilePath, dirPath);
}
}
catch (e) {
console.log('*** Erro in code ', e)
}
}
module.exports = {saveData,};
if any help me in this. it would be great for me.
in this code, i am trying to convert a blob into video format. but sometime it is working and sometime not. can anyone help me in this?
I managed to get the data on #1, but I can't print it out in Express #2, how can I resolve the data and output it?
I believe this is my code error where I can't place the correct async/await.
Here's my code:
minify.js
const { getContent } = require('./get');
const { minifyJS } = require('./processing');
async function getMinify(req, res) {
try {
const remoteUrl = 'http://example.com/script.js';
console.log('URL: ' + remoteUrl);
// #1
const content = await getContent(remoteUrl);
// #2
const outputJS = await minifyJS(content);
res.end(outputJS);
} catch (error) {
res.end(error.content);
}
}
module.exports = { getMinify }
get.js
const got = require('got');
async function getContent(remoteUrl) {
const code = await got(remoteUrl);
return code.body;
}
module.exports = { getContent }
processing.js
const { minify } = require('uglify-js');
async function minifyJS(data) {
const result = await minify(data);
return result.code;
}
module.exports = { minifyJS }
app.js
const express = require('express');
const { getMinify } = require('./minify');
const app = express();
app.get('/*', getMinify);
app.listen(5000);
based on uglify-js, result of minify(data) function has two object code and error.
and minify(data) don't need to await
processing.js should changed
const { minify } = require("uglify-js");
async function minifyJS(data) {
let { error, code } = minify(data);
if (error)
throw error;
return code;
}
module.exports = { minifyJS };
and for handling of two .js file (get.js and processing.js) changed
minify.js
try {
const remoteUrl = 'http://example.com/script.js';
console.log('URL: ' + remoteUrl);
// #1
const content = await getContent(remoteUrl);
// #2
const outputJS = await minifyJS(content);
res.end(outputJS);
} catch (error) {
res.end(error); // remove .content
}
There is a folder with a lot of JSON files and all of them got an object called "name"
I want to get their strings and turn them into a string like this
name0=UsernameExample;name1=Flowers;name2=Test; ...
the number after name is the index/count of the json, like if its name48, its the 48th json
This far I only tried to read the JSONs from the folder but I failed of course
let s = "";
fs.readdir('/tmp/userdb/', (files) => {
files.each(file => {
name = file[file.keys()[0]];
})})
I can already convert this
var other_users = (serialize({
"sid0": 0,
"name0": "user1",
"pays0": "8521",
"avatar0": "357",
"onlinescore0": "50"
}));
to this:
sid0=0;name0=user1;pays0=8521;avatar0=357;onlinescore0=50
with this const
const serialize = obj =>
Object.entries(obj).map(([k, v]) => `${k}=${v}`).join(';')
And I want to send the result to the user with this way
if (req.query.d === 'getRandomPlayers') {
var sessionid = req.body.player_sid
let user = require("./tmp/userdb/" + sessionid + ".json")
var current_user = (serialize({
player_name: user.name
}));
res.send("method_id=1665;" + current_user);
}
It should be like res.send("method_id=1665;" + current_user + thefinalresult);
thefinalresult is what this all should go. current_user and other stuff is not related to this question.
Assuming an example JSON files inside /tmp/userdb/ has the following structure,
{
"53874745": {
"avatar": "372",
"name": "BILLY",
"onlinescore": "1",
"pays": "8758"
}
}
you could do something like the following:
const { promisify } = require("util");
const fs = require("fs");
const path = require("path");
const readdir = promisify(fs.readdir);
const readFile = promisify(fs.readFile);
async function process(excludedSessionId) {
try {
const entries = [];
// get a list of all `JSON` files
const jsonFiles = await readdir(
path.join(__dirname, "./tmp/userdb/")
).then(
(files) => files.filter(
(file) => path.extname(file) === ".json" && !file.includes(excludedSessionId)
)
);
// iterate through a list of all `JSON` files & read their content
for (const [index, file] of jsonFiles.entries()) {
const content = await readFile(
path.join(__dirname, "./tmp/userdb/", file)
).then(JSON.parse);
// create an object for a new entry
const key = `sid${index}`;
const keyValue = Object.keys(content)[0];
// use the `spread syntax` to include the rest of the
// properties in a new entry
const entry = {
[key]: keyValue,
...content[keyValue],
};
entries.push(entry);
}
console.log(entries[0]);
// {
// sid0: '53874745',
// avatar: '372',
// name: 'BILLY',
// onlinescore: '1',
// pays: '8758'
// }
const result = entries.map((entry) => serialize(entry)).join(";");
console.log(result);
// sid0=53874745;avatar=372;name=BILLY;onlinescore=1;pays=8758;
// sid1=154261758;avatar=480;name=JESSEY;onlinescore=30;pays=8521;
return result;
} catch (error) {
console.error(error);
throw error;
}
}
process("154261742");
Then, if you'd want to use this function in a callback of your route controller, you could do something like the following:
app.get("/user", (req, res) => {
// ...
const excludedSessionId = req.body.player_sid;
process(excludedSessionId)
.then(result => {
res.send(result);
})
.catch(error => {
res.status(500).send("Something went wrong.");
});
});
References:
Spread syntax (...) - MDN
async function - MDN
I am trying to compile an array out of data present in some specific files, using a recursive approach to read directories and the file system methods are asynchronous. I am unable to figure out the apt place for the callback invocation.
const fs = require('fs');
const ENTRY = "../a/b";
const FILE_NAME = 'index.json';
var nodes = [];
function doThisOnceDone() {
console.log(nodes);
}
function readFile(path) {
fs.readFile(path + '/' + FILE_NAME,{
encoding:"UTF-8"
}, function(err, data) {
if(err) {
return;
}
nodes.push(data);
});
}
function compileArray(path, callback) {
fs.readdir(path, {
encoding:"UTF-8"
}, function(err, files) {
if(err) {
console.error(err);
return;
}
files.forEach(function(file) {
var nextPath = path + '/' + file;
fs.stat(nextPath, function(err, stats) {
if(err) {
return;
}
if(stats.isDirectory()) {
if(file === 'specific') {
readFile(nextPath);
}
else {
compileArray(nextPath);
}
}
});
});
});
}
compileArray(ENTRY, doThisOnceDone);
When do I know that the recursion tree has been done with , and I can access the nodes array ?
Try this
const util = require('util');
const fs = require('fs');
const stat = util.promisify(fs.stat);
const readFile = util.promisify(fs.readFile);
const readdir = util.promisify(fs.readdir);
const ENTRY = "../a/b";
const FILE_NAME = 'index.json';
var nodes = [];
const compileArray = async (path) => {
try {
const files = await readdir(path);
files.forEach((file) => {
try {
var nextPath = path + '/' + file;
const stats = await stat(nextPath); //check other conditions
if (file === 'specific') {
const data = await readFile(path + '/' + FILE_NAME, {
encoding: "UTF-8"
});
nodes.push(data);
}
} catch (error) {
console.log(error);
}
});
} catch (error) {
console.log(error);
}
}
const util = require('util');
const fs = require('fs');
const stat = util.promisify(fs.stat);
const readFile = util.promisify(fs.readFile);
const readdir = util.promisify(fs.readdir);
const ENTRY = "../a/b";
const FILE_NAME = 'index.json';
var nodes = [];
const compileArray = async (path) => {
try {
const files = await readdir(path);
files.forEach(async (file) => {
try {
var nextPath = path + '/' + file;
const stats = await stat(nextPath); //check other conditions
if (file === 'specific') {
const data = await readFile(path + '/' + FILE_NAME, {
encoding: "UTF-8"
});
nodes.push(data);
}
} catch (error) {
console.log(error);
}
});
} catch (error) {
console.log(error);
}
}
compileArray(ENTRY)