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

156
Views
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 answers
Answer question

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 Report

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 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!