0
0
Node.jsframework~10 mins

Response time optimization in Node.js - Interactive Code Practice

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

Complete the code to create a simple HTTP server that responds quickly.

Node.js
import http from 'http';

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end([1]);
});

server.listen(3000);
Drag options to blanks, or click blank then click option'
Aconsole.log('Hello')
Bawait fetch('data')
C'Hello, world!'
Dres.write('Hello')
Attempts:
3 left
💡 Hint
Common Mistakes
Using asynchronous calls inside res.end()
Trying to log instead of sending response
2fill in blank
medium

Complete the code to cache the response data in memory for faster subsequent responses.

Node.js
let cache = null;

const server = http.createServer((req, res) => {
  if (cache) {
    res.end(cache);
  } else {
    const data = 'Cached response';
    cache = [1];
    res.end(data);
  }
});
Drag options to blanks, or click blank then click option'
Anull
Bawait data
Cres.end(data)
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning res.end(data) to cache
Using await incorrectly
3fill in blank
hard

Fix the error in the asynchronous data fetching to improve response time by caching the result.

Node.js
let cache = null;

async function fetchData() {
  if (cache) return cache;
  cache = await [1];
  return cache;
}

const server = http.createServer(async (req, res) => {
  const data = await fetchData();
  res.end(data);
});
Drag options to blanks, or click blank then click option'
Afetch('https://api.example.com/data').then(res => res.text())
Bfetch('https://api.example.com/data')
Cres.end('data')
D'https://api.example.com/data'
Attempts:
3 left
💡 Hint
Common Mistakes
Caching the fetch promise directly
Using string URL instead of fetch call
4fill in blank
hard

Fill both blanks to implement compression middleware for faster response delivery.

Node.js
import express from 'express';
import [1] from 'compression';

const app = express();
app.use([2]());

app.get('/', (req, res) => {
  res.send('Hello with compression');
});
Drag options to blanks, or click blank then click option'
Acompression
Bcors
Dhelmet
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong middleware like cors or helmet
Not calling the middleware function
5fill in blank
hard

Fill all three blanks to implement a simple cache with expiration for API responses.

Node.js
const cache = new Map();
const CACHE_TIME = 60000; // 1 minute

async function getData(key) {
  const cached = cache.get(key);
  if (cached && (Date.now() - cached.timestamp) < [1]) {
    return cached.value;
  }
  const value = await fetch(key).then(res => res.text());
  cache.set(key, { value: [2], timestamp: [3] });
  return value;
}
Drag options to blanks, or click blank then click option'
ACACHE_TIME
Bvalue
CDate.now()
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for cache values
Not setting timestamp correctly