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

159
Visualizações
How can I add event listener in node js

I'm trying to build a simple calculator where the operations will be done in a Node.js server. How can I add an event listener in the HTML page buttons so that when a button is clicked it does the calculation in the Node.js server?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The title is incorrect, as some users pointed out and you should throw some code snippets to showcase what you've been doing and where exactly your problem is. From the form of the question and details, I would suggest you do some research into the differences between frontend and backend and the solution will be clearer. But regarding your specific issue, assuming you're using Node, probably with Express and some templating engine;

  • You'll want to attach a onClick listener to your button in your ejs/pug/haml file
  • Get the data from your form in some way, either by getElementById or FormData
  • Do a fetch with POST on the endpoint hitting your route -> controller with the data from your form or buttons
  • res.json(...) or, more likely res.render(...) your page with the related data result
about 4 years ago · Juan Pablo Isaza Relatório

0

Check out Node's EventEmitter. With this class, you can emit events and listen for them. You can also create your own classes which extend EventEmitter and make use of its functionality like so:

const { EventEmitter } = require('events');

class MyClass extends EventEmitter {
    constructor() {
        super();
    }

    doSomething() {
        this.emit('someEvent');
    }
}

const test = new MyClass();

test.on('someEvent', () => console.log('event was fired'));

test.doSomething();
about 4 years ago · Juan Pablo Isaza 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