Node.js Event Emitters HTTP Analytics through an Event Emitter


In the HTTP server code (e.g. server.js):

const EventEmitter = require('events')
const serverEvents = new EventEmitter()

// Set up an HTTP server
const http = require('http')
const httpServer = http.createServer((request, response) => {
  // Handler the request...
  // Then emit an event about what happened
  serverEvents.emit('request', request.method, request.url)

// Expose the event emitter
module.exports = serverEvents

In supervisor code (e.g. supervisor.js):

const server = require('./server.js')
// Since the server exported an event emitter, we can listen to it for changes:
server.on('request', (method, url) => {
  console.log(`Got a request: ${method} ${url}`)

Whenever the server gets a request, it will emit an event called request which the supervisor is listening for, and then the supervisor can react to the event.