I'm trying to understand how can I read a file in js So I start to use node js for use the fs package
So I'm doing some thing like this, it works :
var fs = require('fs');
function allTopics() {
const result = new Map();
try {
const fileContent = fs.readFileSync( "../svg/plan.svg","UTF-8");
const lines = fileContent.split("\n");
const listTickets = retrieveTopic(lines);
listTickets.forEach(function (value, i) {
result.set(i, value);
});
} catch (err) {
return Promise.reject("ERROR: Cannot find file");
}
console.log(result);
return result;
}
function retrieveTopic(lines) {
const listTopic = [];
lines.forEach((line) => {
const topicMatch = line.match(/\{[\w\/]+\}/g);
if (topicMatch) {
listTopic.push(topicMatch[0]);
}
})
return listTopic;
}
allTopics();
module.exports = { allTopics };
So now what I want, it's to use this but into a html file for see in browser into console :
<script>
function allTopics() {
const result = new Map();
try {
const fileContent = fs.readFileSync( "../svg/plan.svg","UTF-8");
const lines = fileContent.split("\n");
const listTickets = retrieveTopic(lines);
listTickets.forEach(function (value, i) {
result.set(i, value);
});
} catch (err) {
return Promise.reject("ERROR: Cannot find file");
}
console.log(result);
return result;
}
function retrieveTopic(lines) {
const listTopic = [];
lines.forEach((line) => {
const topicMatch = line.match(/\{[\w\/]+\}/g);
if (topicMatch) {
listTopic.push(topicMatch[0]);
}
})
return listTopic;
}
The problem is that you can't use fs into browser, so I can read a file and see the output into a browser console
I don't wanna use something like input and then choosen file
Thanks for your help