Node.js 101 – Events

Node.js Events

Much of the Node.js core is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects (called “emitters”) emit named events that cause Function objects (“listeners”) to be called.

The following …


This content originally appeared on DEV Community and was authored by Eric Chapman

Node.js Events

Much of the Node.js core is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects (called "emitters") emit named events that cause Function objects ("listeners") to be called.

The following example shows a simple EventEmitter with a single listener that occur when for example a sale is made

const EventEmitter = require('events');

const myEmitter = new EventEmitter()

myEmitter.on('newSale', () => {
  console.log('A new sale occur')
})

myEmitter.emit('newSale')

The eventEmitter.on() method is used to register listeners, while the eventEmitter.emit() method is used to trigger the event.

Passing arguments to listeners

The eventEmitter.emit() method allows an arbitrary set of arguments to be passed to the listener functions

const EventEmitter = require('events');

const myEmitter = new EventEmitter()

myEmitter.on('newSale', (total) => {
  console.log(`A new sale occur total of: ${price}`)
})

myEmitter.emit('newSale', 599.99)

Node.j server work with eventEmitter

Now that we know about Node.js events. We are able to better understand the logic of the Node.js server object.

const server = http.createServer()

// this will create a event name request
server.on('request', (req, res) => {
  // when Node.js server trigger a request event this message will display
  res.end('Request received')
})

// this will loop and wait for events
server.listen(5000, '127.0.0.1', () => {
  console.log('Waiting for request')
})

Conclusion

That's it for today. Tomorrow the journey continue. Stay tune!

Follow me on Twitter:


This content originally appeared on DEV Community and was authored by Eric Chapman


Print Share Comment Cite Upload Translate Updates
APA

Eric Chapman | Sciencx (2021-04-07T14:09:35+00:00) Node.js 101 – Events. Retrieved from https://www.scien.cx/2021/04/07/node-js-101-events/

MLA
" » Node.js 101 – Events." Eric Chapman | Sciencx - Wednesday April 7, 2021, https://www.scien.cx/2021/04/07/node-js-101-events/
HARVARD
Eric Chapman | Sciencx Wednesday April 7, 2021 » Node.js 101 – Events., viewed ,<https://www.scien.cx/2021/04/07/node-js-101-events/>
VANCOUVER
Eric Chapman | Sciencx - » Node.js 101 – Events. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/07/node-js-101-events/
CHICAGO
" » Node.js 101 – Events." Eric Chapman | Sciencx - Accessed . https://www.scien.cx/2021/04/07/node-js-101-events/
IEEE
" » Node.js 101 – Events." Eric Chapman | Sciencx [Online]. Available: https://www.scien.cx/2021/04/07/node-js-101-events/. [Accessed: ]
rf:citation
» Node.js 101 – Events | Eric Chapman | Sciencx | https://www.scien.cx/2021/04/07/node-js-101-events/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.