1
app.get("/" ,function(req,res){
   //  console.log(req)
   console.log(__dirname + '/index.html ');
   res.sendFile(__dirname + '/index.html ');
});

The server and html is in same directory but when server is started it gives me the following Error:

ENOENT: no such file or directory, stat 'F:\web development\calculator \index.html '

coreuter
  • 3,331
  • 4
  • 28
  • 74
Sharma Ankush
  • 301
  • 3
  • 7

2 Answers2

0

You have a space in 'index.html' when calling the sendFile function. Replace the res.sendFile line with this -

res.sendFile(__dirname +'/index.html');

I think it's a simple typo error.

  • check this quesiton bro, I think you will find the answer, let me know if you still don't find it - https://stackoverflow.com/questions/4529586/render-basic-html-view – Md. Shahla Zulkarnine Jul 20 '21 at 18:31
  • @SharmaAnkush Check the complete path again. It seems that there is another whitespace after `\calculator \`. Try renaming the folder "calculator" and remove that space, too. Additionally check this answer: https://stackoverflow.com/questions/16395612/unable-to-execute-child-process-exec-when-path-has-spaces - maybe you need to escape the whitespaces in your path?! – coreuter Jul 21 '21 at 11:43
0

Your question doesn't include enough information for us to give a definitive answer. When possible, we like to plug your code into our own system to try to duplicate the problem.

Doing a "best guess" on my end; I'm assuming this is a javascript issue using the Express module. I was able to determine the path that express sees by using the following from index.js, which is referenced from my index.html:

const loc = window.location.pathname;
document.writeln(loc)

I was then able to see the result by entering the following path into my web browser:

    file:///home/vikingglen/javascript/myapp/index.html

The following was displayed on my web page:

/home/vikingglen/javascript/myapp/index.html

Plugging that path into app.js, using the following code, I was able to access index.html:

const express = require('express')
const app = express()
const port = 3000

__dirname = '/home/vikingglen/javascript/myapp/'

app.get("/", function (req, res) {
    console.log(__dirname + 'index.html');
    res.sendFile(__dirname + 'index.html');
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

As a side note, I prefer to put a "/" at the end of directory paths so I can quickly tell it's a directory:

_dirname = '/home/vikingglen/javascript/myapp/'

instead of:

_dirname = '/home/vikingglen/javascript/myapp'

If this doesn't solve your problem, then you'll need to send us enough of your code so we can plug it into our own systems to reproduce the error.

VikingGlen
  • 1,705
  • 18
  • 18