'How to resolve Nodejs: Error: ENOENT: no such file or directory - Pls help im a beginner

I was doing a online course in youtube, and i followed exactly the same steps as the guy, and when i encountered this error i even re-watched the same part over and over again, tried to find in google, readed alot of the same error's in stackoverflow and other sites, and couldn't solve it. I'm a beginner, im only studying and if anyone can help me, i would apreciate. I have those arquives where this issue could be: server.js, script.js, style.css

server.js

const http = require('http')
const fs = require('fs')
const path = require('path')

http.createServer((req, res) => {

    const file = req.url === '' ? 'index.html' : req.url
    const filePath = path.join(__dirname, 'public', file)
    const extname = path.extname(filePath)

    const allowedFileTypes = ['.html', '.css', '.js']
    const allowed = allowedFileTypes.find(item => item == extname)

    if(!allowed) return

    fs.readFile(
        filePath,
        (err, content) => {
            if(err) throw err

            res.end(content)
        }
    )

}).listen(5000, () => console.log('Server is running'))

script.js

const ul = document.querySelector("ul")
const input = document.querySelector("input")
const form = document.querySelector('form')


function addElement({ name, url }) {
    const li = document.createElement('li')
    const a = document.createElement("a")
    const trash = document.createElement("span")

    a.href = url
    a.innerHTML = name
    a.target = "_blank"

    trash.innerHTML = "x"
    trash.onclick = () => removeElement(trash)

    li.append(a)
    li.append(trash)
    ul.append(li)
}

function removeElement(el) {
    if (confirm('Tem certeza que deseja deletar?'))
        el.parentNode.remove()
}

form.addEventListener("submit", (event) => {
    event.preventDefault();

    let { value } = input

    if (!value) 
        return alert('Preencha o campo')

    const [name, url] = value.split(",")

    if (!url) 
        return alert('formate o texto da maneira correta')

    if (!/^http/.test(url)) 
        return alert("Digite a url da maneira correta")

    addElement({ name, url })

    input.value = ""
})

style.css

@import url('https://fonts.googleapis.com/css2?family=Quicksand:wght@300;600&display=swap');

* {
  box-sizing: border-box;
}

html {
  font-size: 62.5%;
}

body {
  font-family: 'Quicksand', sans-serif;
  
  background: #7159c1;
}

.container {
  width: 80%;
  max-width: 400px;
  
  margin: auto;
}

h1 {
  text-align:center;
  color: white;
  
  font-size: 3.4rem;
}

input {
  width: 100%;
  
  padding: 8px 16px;
  margin-bottom: 32px;
  
  border-radius: 16px;
  border: 1px solid #ccc;
  
  outline: none;
  
  font-size: 1.6rem;
  font-weight:300;
}

ul {
  background: white;
  
  box-shadow: 0px 4px 8px -2px #00000033;
  border-radius: 6px;
  border: 1px solid #ddd;
  
  padding: 16px;
  
  font-size: 1.4rem;
  
}

li {
  list-style: none;
  
  display:flex;
  align-items: center;
  justify-content: space-between;
  
  border-bottom: 1px solid #ddd;
}


a {
  display: block;
  
  color: #333;
  text-decoration: none;  
  
  padding: 16px 0;
  font-size: 1.8rem;
  
}

a:hover {
  font-weight: bold;
}

li:first-child,
li:last-child {
  border: none;
}

li span {
  cursor:pointer;
}


Solution 1:[1]

I'd just return res.end() on 2 conditions, the first being the given !allowed and the second being if the FILE doesn't exist(the error is thrown because fs was trying to access a file that doesn't exist).. the only thing being edited is your server.js

const http = require('http')
const fs = require('fs')
const path = require('path')
const isFile=(path)=>fs.lstatSync(path).isFile() //checks if something is a file or not

http.createServer((req, res) => {

    const file = req.url === '' ? 'index.html' : req.url
    const filePath = path.join(__dirname, 'public', file)
    const extname = path.extname(filePath)

    const allowedFileTypes = ['.html', '.css', '.js']
    const allowed = allowedFileTypes.find(item => item == extname)

    if(!allowed){return res.end()} //res.end to END the request
    let existingFiles=fs.readdirSync().filter(isFile)
    if(!existingFiles.includes(path)){
        console.log("faulty path requested",path)
        //I'm betting your index.html is causing this so that's why I'm showing what path would've been attempted
        return res.end() //res.end to END the request
    }

    fs.readFile(
        filePath,
        (err, content) => {
            if(err) throw err

            res.end(content)
        }
    )

}).listen(5000, () => console.log('Server is running'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The Bomb Squad