Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
How to point to Html folder from path module Node Js

I want to send html file as response using express so to do that I need to give the position or path of index.html file.

so this is my file structure -

MyWebsite 
     Html
        index.html
     Css
        index.css
     Js
       index.js

if you still not understanding the file structure this the image of file structure :

enter image description here

so in my JavaScript I have written something like this :

import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const app = express();

const port = process.env.port || 8080;

app.get('/', (req, res) => {
  const htmlFile = path.join(__dirname, '/index.html');
  res.sendFile(htmlFile)
});

app.listen(port);

giving error -

Error: ENOENT: no such file or directory, stat 'F:\Websites\MyWebsite\js\index.html'

It is not working!. I am unable to give the path or folder name! how can I get the index.html folder's path

any help will be appreciated!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

its simple you have set back-word so server can find you index.html change your code to

app.get('/', (req, res) => {
   const htmlFile = path.join(__dirname, '/index.html');
   res.sendFile(htmlFile)
});

new Code Will Be

app.get('/', (req, res) => {
  const htmlFile = path.join(__dirname, '../html/index.html');
  res.sendFile(htmlFile)
});

i hope its work for you its work for me

about 4 years ago · Juan Pablo Isaza Relatório

0

you can use 'path' module to move one directory back.

path.join(__filename, '..', 'html', 'index.html')

this way you will avoid slashes and eliminate problems while running your app in different operating systems.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda