01-edu / public

📚 @01-edu's Public Repository
http://public.01-edu.org/
214 stars 440 forks source link

friend-support subject #2425

Closed Pomog closed 7 months ago

Pomog commented 7 months ago

friend-support There is an Error with tests Because this code works

import http from "http";
import fs from "fs";

const PORT = 5000;
const GUESTS_DIRECTORY = "./guests/";
// const GUESTS_DIRECTORY = "./";

const server = http.createServer((req, res) => {
  console.log(`Server listening on port ${PORT}`);

  // Extract guest name from request URL
  const guestName = req.url.substring(1); // Remove leading '/'

  // Read guest JSON file
    fs.readFile(`${GUESTS_DIRECTORY}${guestName}.json`, "utf8", (err, data) => {
    //   fs.readFile(`}${guestName}.json`, "utf8", (err, data) => {
    if (err) {
      if (err.code === "ENOENT") {
        // Guest not found
        res.writeHead(404, { "Content-Type": "application/json" });
        res.end(JSON.stringify({ error: "guest not found" }));
      } else {
        // Server error
        res.writeHead(500, { "Content-Type": "application/json" });
        res.end(JSON.stringify({ error: "server failed" }));
      }
    } else {
      // Send guest details
      res.writeHead(200, { "Content-Type": "application/json" });
      res.end(data);
    }
  });
});

server.listen(PORT);

image

nprimo commented 7 months ago

@Pomog, could you please share the error you're getting? :)

Pomog commented 7 months ago

Error: Already failed code at commit bcadcbf

Previous output: timeout: Did you write an infinite loop?

<html>
<body>
<!--StartFragment-->
import http from "http";
--
  | import fs from "fs";
  |  
  | const PORT = 5000;
  | const GUESTS_DIRECTORY = "./guests/";
  | // const GUESTS_DIRECTORY = "./";
  |  
  | const server = http.createServer((req, res) => {
  | console.log(`Server listening on port ${PORT}`);
  |  
  | // Extract guest name from request URL
  | const guestName = req.url.substring(1); // Remove leading '/'
  |  
  | // Read guest JSON file
  | fs.readFile(`${GUESTS_DIRECTORY}${guestName}.json`, "utf8", (err, data) => {
  | //   fs.readFile(`}${guestName}.json`, "utf8", (err, data) => {
  | if (err) {
  | if (err.code === "ENOENT") {
  | // Guest not found
  | res.writeHead(404, { "Content-Type": "application/json" });
  | res.end(JSON.stringify({ error: "guest not found" }));
  | } else {
  | // Server error
  | res.writeHead(500, { "Content-Type": "application/json" });
  | res.end(JSON.stringify({ error: "server failed" }));
  | }
  | } else {
  | // Send guest details
  | res.writeHead(200, { "Content-Type": "application/json" });
  | res.end(data);
  | }
  | });
  | });
  |  
  | server.listen(PORT);

<!--EndFragment-->
</body>
</html>
nprimo commented 7 months ago
  • It has to listen on port 5000, and it will have to print a simple message on the console, specifying the listening port;

Your solution looks fine, but you should check this point. I will add a small change to clarify: the message should be displayed as soon as it starts (if not it means you already know which port the server is listening to ;)

Pomog commented 7 months ago

thanks, solved

server.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
});

confusing task

Pomog commented 7 months ago

thanks