简体   繁体   中英

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

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 '

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM