Visual Studio Debug Error: ENOENT: no such file or directory, open

Viewed 1366

I am building a node server (without Express), and have an issue that ONLY appears during debugging, and working fine otherwise.

Basically, it cannot find the file (index.html) I am trying to serve, and gives me:

"Sorry, check with the site admin for error: Error: ENOENT: no such file or directory, open './public/index.html' .."

Code (switch routing, then req response)

switch (filePath) {
  case "./main.css":
    filePath = "./public/main.css";
    res.writeHead(200, { "Content-type": "text/css" });
    break;
  case "./main.js":
    filePath = "./public/main.js";
    res.writeHead(200, { "Content-type": "application/javascript" });
    break;
  case "./":
    filePath = "./public/index.html"; //FILE TRYING TO SERVE <====
    res.writeHead(200, { "Content-type": "text/html" });
}

fs.readFile(filePath, function (error, content) {
  if (error) {
    if (filePath == "./favicon.ico") {
      res.writeHead(200, { "Content-Type": "image/x-icon" });
      res.end();
      console.log("favicon reqed");
      return;
    } else {
      // BELOW IS THE ERROR MESSAGE <=========
      res.end(
        "Sorry, check with the site admin for error: " + error + " ..\n"
      );

      console.log("file path " + filePath);
      // outputs: "file path ./public/index.html" as I expected

      console.log("current dir " + __dirname);
      // outputs correct directory here.
    }
  } else {
    res.end(content, "utf-8");
  }
});

launch.json debugger configuration

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "skipFiles": ["<node_internals>/**"],
  "program": "${workspaceFolder}/...PATH/IS/CORRECT.../milestone2_app/app.js",
  "console": "integratedTerminal"
}

I am puzzled as the app works fine if I just run >node app.js without the debugger, so I don't think pathing is the problem. I am running vscode on macOS if that matters.

Thanks for any inputs,

0 Answers
Related