Bird
Raised Fist0
Node.jsframework~10 mins

Long polling as fallback in Node.js - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple HTTP server in Node.js.

Node.js
const http = require('http');
const server = http.createServer((req, res) => {
  res.end('Hello World');
});
server.listen([1]);
Drag options to blanks, or click blank then click option'
Ahttp
B3000
Clocalhost
Dserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like 'http' instead of a port number.
Passing the server object instead of a port.
2fill in blank
medium

Complete the code to set the response header for JSON content.

Node.js
res.writeHead(200, { 'Content-Type': [1] });
Drag options to blanks, or click blank then click option'
A'application/json'
B'text/html'
C'text/plain'
D'image/png'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/html' when sending JSON data.
Forgetting to set the Content-Type header.
3fill in blank
hard

Fix the error in the long polling function to correctly send a response when data is ready.

Node.js
function longPoll(req, res) {
  if (dataReady) {
    res.[1](JSON.stringify({ message: 'Data ready' }));
  } else {
    // wait and retry
  }
}
Drag options to blanks, or click blank then click option'
AwriteHead
Bwrite
Csend
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.write() without ending the response.
Using res.send() which is not a native Node.js method.
4fill in blank
hard

Fill both blanks to correctly implement a long polling retry with a timeout.

Node.js
function longPoll(req, res) {
  if (dataReady) {
    res.end(JSON.stringify({ data: 'Here is your data' }));
  } else {
    setTimeout(() => {
      [1](req, res);
    }, [2]);
  }
}
Drag options to blanks, or click blank then click option'
AlongPoll
BsendData
C5000
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a wrong function name.
Using too long or too short timeout values.
5fill in blank
hard

Fill all three blanks to create a fallback long polling server that responds with JSON data.

Node.js
const http = require('http');
let dataReady = false;

const server = http.createServer((req, res) => {
  if (req.url === '/poll') {
    res.writeHead(200, { 'Content-Type': [1] });
    if (dataReady) {
      res.[2](JSON.stringify({ status: 'ready' }));
      dataReady = false;
    } else {
      setTimeout(() => {
        res.[3](JSON.stringify({ status: 'timeout' }));
      }, 3000);
    }
  } else {
    res.writeHead(404);
    res.end();
  }
});

server.listen(8080);
Drag options to blanks, or click blank then click option'
A'application/json'
Bend
Cwrite
D'text/plain'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/plain' instead of 'application/json' for JSON data.
Using res.write() without ending the response.

Practice

(1/5)
1. What is the main purpose of using long polling in Node.js applications?
easy
A. To keep a client request open until new data is available
B. To immediately close the connection after sending data
C. To send multiple responses to a single request
D. To replace WebSocket connections entirely

Solution

  1. Step 1: Understand long polling behavior

    Long polling keeps the client request open until the server has new data to send.
  2. Step 2: Compare options to this behavior

    Only To keep a client request open until new data is available describes this behavior correctly; others describe different or incorrect behaviors.
  3. Final Answer:

    To keep a client request open until new data is available -> Option A
  4. Quick Check:

    Long polling = open request until data ready [OK]
Hint: Long polling holds request open until data arrives [OK]
Common Mistakes:
  • Thinking long polling sends multiple responses per request
  • Confusing long polling with WebSocket persistent connection
  • Assuming long polling closes connection immediately
2. Which of the following is the correct way to implement a long polling endpoint in Node.js using Express?
easy
A. res.write('data'); // keep connection open without res.end()
B. res.send('data'); res.end();
C. res.json('data'); res.end();
D. res.send('data'); // then wait before closing connection

Solution

  1. Step 1: Identify how to keep connection open

    In long polling, the server holds the connection open, so it should not call res.end() immediately.
  2. Step 2: Check which option keeps connection open

    res.write('data'); // keep connection open without res.end() uses res.write() without res.end(), which keeps the connection open for long polling.
  3. Final Answer:

    res.write('data'); // keep connection open without res.end() -> Option A
  4. Quick Check:

    Keep connection open = no res.end() [OK]
Hint: Use res.write() without res.end() to keep connection open [OK]
Common Mistakes:
  • Calling res.end() immediately closes connection
  • Using res.send() closes connection right away
  • Not sending any response causes timeout
3. Consider this Node.js long polling code snippet:
app.get('/poll', (req, res) => {
  setTimeout(() => {
    res.json({ message: 'Update available' });
  }, 5000);
});
What will the client experience when calling this endpoint?
medium
A. The client never receives a response
B. The client receives the update immediately
C. The client waits 5 seconds, then receives the update
D. The client receives multiple updates every 5 seconds

Solution

  1. Step 1: Analyze the setTimeout delay

    The server waits 5 seconds before sending the JSON response.
  2. Step 2: Understand client wait behavior

    The client request stays open during the 5 seconds, then receives the update once sent.
  3. Final Answer:

    The client waits 5 seconds, then receives the update -> Option C
  4. Quick Check:

    setTimeout delay = delayed response [OK]
Hint: setTimeout delays response, client waits that time [OK]
Common Mistakes:
  • Assuming immediate response despite delay
  • Expecting multiple responses from one request
  • Thinking client never gets response
4. Given this long polling code snippet, what is the main issue?
app.get('/poll', (req, res) => {
  res.json({ message: 'Update' });
  setTimeout(() => {
    res.json({ message: 'Another update' });
  }, 3000);
});
medium
A. The first res.json() never sends data
B. Calling res.json() twice causes an error
C. setTimeout is not asynchronous
D. The server never responds to the client

Solution

  1. Step 1: Understand res.json() behavior

    res.json() sends a response and closes the connection immediately.
  2. Step 2: Analyze calling res.json() twice

    Calling res.json() a second time after the connection is closed causes an error.
  3. Final Answer:

    Calling res.json() twice causes an error -> Option B
  4. Quick Check:

    Multiple res.json() calls = error [OK]
Hint: Only one res.json() per request allowed [OK]
Common Mistakes:
  • Thinking setTimeout blocks code
  • Assuming multiple res.json() calls send multiple responses
  • Ignoring connection close after first response
5. You want to implement long polling as a fallback in a Node.js app that first tries WebSocket. Which approach correctly handles fallback and client reconnection?
hard
A. Use WebSocket and long polling simultaneously, sending duplicate data to client
B. Use WebSocket only; if unavailable, do nothing and drop updates
C. Use long polling only; never try WebSocket to simplify code
D. Use WebSocket; if unavailable, client repeatedly sends GET requests that wait on server until data or timeout, then reconnect

Solution

  1. Step 1: Understand fallback strategy

    WebSocket is preferred for real-time, but fallback uses long polling with repeated client requests.
  2. Step 2: Identify correct client reconnection method

    The client must send repeated GET requests that wait on server until data or timeout, then reconnect to keep updates flowing.
  3. Final Answer:

    Use WebSocket; if unavailable, client repeatedly sends GET requests that wait on server until data or timeout, then reconnect -> Option D
  4. Quick Check:

    Fallback = repeated long polling requests [OK]
Hint: Fallback means repeated long polling requests after WebSocket fails [OK]
Common Mistakes:
  • Ignoring fallback and dropping updates
  • Sending duplicate data by using both methods simultaneously
  • Not reconnecting client after long polling timeout
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard