Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

348
Views
Using simple scripts written in Node JS by invoking in HTML

I'm kinda newbie in terms of any backend and have a 'little' problem with Node.JS. I have a site built with handlebars.js templates system, and wanted to achieve active changing of 'data base' written in .json file. I have a list of articles with titles etc. and wanted to wrote a very simple panel to be able to add anything to this database. I wrote a very simple script in node.js, which works fine when launching through console. But I want to be able to activate it through simple push of button on client side.

html part is irrelevant, so let's say i have simple button

<button id="button1"></button>

articlesData.json

 {
  "articles": [
    {
      "title": "123",
      "author": "Krzysztof",
      "category": "Geography",
      "date": "15-04-2017",
      "imgsrc": "public/img/office.jpg",
      "descrSht": "Tym krótkim...",
      "descr": "Tym krótkim wstepem zaczynam swoj blog",
      "adress": "poczatek_bloga",
      "images": {
        "img1": "public/img/office.jpg"
      }
    },
    {
      "title": "asdgsdgsg",
      "author": "Krzysztof",
      "category": "History",
      "date": "12-12-2016",
      "imgsrc": "public/img/office.jpg",
      "descrSht": "Tym krótkim...",
      "descr": "Tym krótkim wstepem zaczynam swoj blog"
    },
    {
      "title": "asdgsdg",
      "author": "Krzysztof",
      "category": "Geography",
      "date": "15-11-2016",
      "imgsrc": "public/img/office.jpg",
      "descrSht": "Tym krótkim...",
      "descr": "Tym krótkim wstepem zaczynam swoj blog"
    }
  ]
}

and here is my node.js script which works through windows/linux terminal only

var fs = require('fs');
var fileName = './data/articlesData.json';
var file = require(fileName);
var title = "Nowy tytul";
var author = "Krzysztof";
var length = file.articles.length;

console.log(length);
file.articles[length] = {title: title, author: author};

fs.writeFile(fileName, JSON.stringify(file, null, 2), function (err) {
if (err) return console.log(err);
console.log(JSON.stringify(file));
console.log('writing to ' + fileName);
});

Any help would be useful, I only want to make it possible to activate it through button click. Thanks in advance

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Tried using like this:

var file = JSON.parse(fs.readFileSync('./data/articlesData.json'), 'utf8');
file.articles.push({title: title, author: author})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!