Refer to the code below:
01 const server = require('server');
02
03 // Insert code here
A developer imports a library that creates a web server. The imported library uses events and callbacks to start the server.
Which code should be inserted at line 03 to set up an event and start the web server?
Comprehensive and Detailed Explanation From Exact Extract JavaScript Knowledge:
The question specifies:
The library ''uses events and callbacks to start the server''.
We want to ''set up an event and start the web server''.
Option C:
server((port) => { console.log('Listening on', port); });
This:
Calls server(...), which (by design in such libraries) typically starts the web server.
Passes a callback function that receives port when the server is ready.
Inside the callback, it logs 'Listening on
This matches the ''events and callbacks'' description: the callback is invoked when the server has started listening.
Why others are incorrect:
A . server.on('connect', ...)
Assumes server is an event emitter instance with a 'connect' event, which is not given. Also, this line alone would not start the server.
B . server.start();
Assumes a start method exists; the question says the library ''uses events and callbacks to start,'' implying invocation with a callback, not a plain start() call.
D . server();
Might start the server, but does not ''set up an event'' or callback to know when or where it is listening.
Therefore, C aligns with the described usage.
Concepts: callback-based APIs, starting servers with callbacks, event/callback style vs simple method calls.
Currently there are no comments in this discussion, be the first to comment!