Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Salesforce JS-Dev-101 Exam - Topic 5 Question 5 Discussion

Actual exam question for Salesforce's JS-Dev-101 exam
Question #: 5
Topic #: 5
[All JS-Dev-101 Questions]

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?

Show Suggested Answer Hide Answer
Suggested Answer: C

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.


Contribute your Thoughts:

0/2000 characters

Currently there are no comments in this discussion, be the first to comment!


Save Cancel