Skip to content

Latest commit

 

History

History
24 lines (18 loc) · 1.25 KB

nodejs.md

File metadata and controls

24 lines (18 loc) · 1.25 KB

What is EventEmitter in Node.js

All objects that emit events are instances of the EventEmitter class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object.

When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously.

What is middlewares? What is the concept of middlewares in Express.js?

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

Execute any code. Make changes to the request and the response objects. End the request-response cycle. Call the next middleware function in the stack. Bind application-level middleware to an instance of the app object by using the app.use() and app.METHOD() functions, where METHOD is the HTTP method of the request that the middleware function handles (such as GET, PUT, or POST) in lowercase.

var app = express()

app.use(function (req, res, next) {
  console.log('Time:', Date.now())
  next()
})