0

Why I get No such a file index.html error

My Folder Structure

enter image description here

here is my Code in server.js.

    const PORT = process.env.PORT || 8000
    const express = require('express');
    const path = require('path')
    const http = require('http');
    const cors = require('cors');
    const app = require('./app.js');
    const planetsRouter = require('./routers/planets/planet.router')
    const { loadPlanetsData } = require('./models/planets.model')
    
    const server = http.createServer(app)
    app.use(cors({ origin: 'http://localhost:3000' }))
    app.use(express.json())
    app.use(express.static(path.join(__dirname, '..', 'public')))
    app.get('/', (req, res) => {
        console.log('Res', req)
        res.sendFile(path.join(__dirname, '..', 'public', 'index.html'))
    })
    app.use(planetsRouter)
    async function startServer () {
    
        await loadPlanetsData()
        server.listen(PORT, () => {
            console.log(`Listening on ${PORT}...`)
        })
    }
    
    startServer()

I got this error Error: ENOENT: no such file or directory, stat 'C:\Users\kullanıcı\Desktop\Node Js\24-Nasa-Project\server\public\index.html'

enter image description here

This is server.js

2 Answers 2

1

If you're getting the error:

Error: ENOENT: no such file or directory, stat 'C:\Users\kullanıcı\Desktop\Node Js\24-Nasa-Project\server\public\index.html'

That means you don't have a file at the path:

C:\Users\kullanıcı\Desktop\Node Js\24-Nasa-Project\server\public\index.html

Just looking at that path, I notice: Users\kullanıcı\Desktop ... that looks like a path from your computer. But you're saying this is on your server ... does your server actually have that folder?

What could be going on is you could have hard-coded that path somewhere in your code during development, and then when you try to use it on your server, it fails because your server doesn't have that path. It doesn't seem possible from the code you provided, but could there be any other code you didn't share?

Alternatively, I noticed you had:

res.sendFile(path.join(__dirname, '..', 'public', 'index.html'))

Why the '..'? It seems your server.js is in your server project root folder, so .. would mean one folder above that .. but the public folder is in the same directory as server.js.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, my code is in the src folder, I will add my folder structure and how to run the server
There is this kind of structure but Language differences can be a reason? Because my computer language is Turkish and users mean kullanıcılar in Turkish. maybe it's not compiled this root because of that
0

I found the solution I made mistake while writing client package.json it was like this

I have Change it to the this enter image description here

There is a space after server/public if ı do like first picture I need to write public joint to && symbol

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.