Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

347
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda