Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda