Understanding the Node.js http.Server.on() Method

Asked 2 years ago, Updated 2 years ago, 68 views

I'm studying Node.js

I am trying to write a simple web server by referring to the following references and blog posts.
https://nodejs.org/api/index.html

Some of the sample codes in the reference, personal blogs, etc. can be written as follows:

const http=require('http');
const server = http.createServer();

server.on(~~)

This method called server.on could not be found on the HTTP page of the reference and
I don't know what the specifications are.

If anyone knows, could you please reply?

node.js

2022-09-30 16:50

1 Answers

Many Node.js built-in classes, including http.Server, inherit EventEmitter, and this .on is part of it.

emitter.on(eventName, listener)

Each event is described in the documentation for each class, where emitter.on takes the event name and listener callback as arguments, as used in the sample.
The callback argument is also in the documentation for each class, for example, (request: http.IncomingMessage, response:http.ServerResponse) is passed as the argument if Event:'request'.

Come to think of it, there were some people who made Node.js class diagram, so please refer to it:
Node.js class diagram with v10.0.0 support - Qiita


2022-09-30 16:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.