0
0
Node.jsframework~10 mins

Routing requests manually 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 basic HTTP server that listens on port 3000.

Node.js
const http = require('http');

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

server.listen([1], () => {
  console.log('Server running');
});
Drag options to blanks, or click blank then click option'
A5000
B3000
C8080
D80
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for the port.
Choosing a port that is already in use.
2fill in blank
medium

Complete the code to check the request URL and respond with 'Home Page' if the path is '/'.

Node.js
const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === [1]) {
    res.statusCode = 200;
    res.end('Home Page');
  } else {
    res.statusCode = 404;
    res.end('Not Found');
  }
});

server.listen(3000);
Drag options to blanks, or click blank then click option'
A'/main'
B'/home'
C'/index'
D'/'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/home' instead of '/' for the home page.
Forgetting to include quotes around the path string.
3fill in blank
hard

Fix the error in the code to respond with 'About Page' when the URL is '/about'.

Node.js
const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === [1]) {
    res.statusCode = 200;
    res.end('About Page');
  } else {
    res.statusCode = 404;
    res.end('Not Found');
  }
});

server.listen(3000);
Drag options to blanks, or click blank then click option'
A'/About'
B'about'
C'/about'
D'about/'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the leading slash in the path string.
Using incorrect case or trailing slashes.
4fill in blank
hard

Fill both blanks to respond with JSON data when the URL is '/api/data'.

Node.js
const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === [1]) {
    res.statusCode = 200;
    res.setHeader('Content-Type', [2]);
    res.end(JSON.stringify({ message: 'Hello API' }));
  } else {
    res.statusCode = 404;
    res.end('Not Found');
  }
});

server.listen(3000);
Drag options to blanks, or click blank then click option'
A'/api/data'
B'text/html'
C'application/json'
D'/data/api'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong URL path or swapping the URL and Content-Type values.
Setting Content-Type to 'text/html' for JSON data.
5fill in blank
hard

Fill all three blanks to route requests to different responses for '/', '/about', and '/contact'.

Node.js
const http = require('http');

const server = http.createServer((req, res) => {
  if (req.url === [1]) {
    res.end('Home Page');
  } else if (req.url === [2]) {
    res.end('About Page');
  } else if (req.url === [3]) {
    res.end('Contact Page');
  } else {
    res.statusCode = 404;
    res.end('Not Found');
  }
});

server.listen(3000);
Drag options to blanks, or click blank then click option'
A'/'
B'/about'
C'/contact'
D'/home'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/home' instead of '/' for the home page.
Mixing up the paths or missing the leading slash.