Node.js · Junior · Question 57 of 75

What does this code do?

import http from "node:http";

http
  .createServer((req, res) => {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.end("Hello");
  })
  .listen(3000);

Explanation

The callback runs for every request, whatever the method or URL. Routing by req.method and req.url is up to you, which is why frameworks like Express exist.

Further reading

Problem with a question

Node.js · Junior · question 57

Test yourself

Take the Node.js · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz