Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!