Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: ENOENT: no such file or directory, stat '/public/main.html' at Error (native)

This is my server.js file:

var express = require('express'),     app = express();   app      .use(express.static('./public'))     .get('*',function (req,res) {         res.sendfile('/public/main.html');         })  .listen(3000); 

This is my main.html:

<!DOCTYPE html> <html>     <head>         <titel>Contacts</titel>     <base href'/'>      </head>     <body>         <div class="container">          <div class="page-header">              <h1>Contatcs</h1>          </div>         </div>      </body> </html> 

And the folder structure:

like image 912
pradeep murugan Avatar asked Mar 31 '16 18:03

pradeep murugan


People also ask

What does error code Enoent mean?

It's an abbreviation of Error NO ENTry (or Error NO ENTity), and can actually be used for more than files/directories. It's abbreviated because C compilers at the dawn of time didn't support more than 8 characters in symbols. Follow this answer to receive notifications. edited Jun 4, 2019 at 14:30. community wiki.

Can not get node JS?

To fix the 'Error: Cannot GET /' message with Node. js and Express, we need to add the route handler for the GET / route. app. get("/", (req, res) => { res.


1 Answers

Since both the server and the index file are INSIDE the "public" directory, you can simply use :

res.sendfile('./main.html'); 

To answer the question in the comments : In Express 4.x, the sendfile method was replaced by the sendFile method (all lowercase -> camelCase). Probably just an oversight in early versions, that got fixed in the latter.

like image 149
Bernard Saucier Avatar answered Sep 21 '22 05:09

Bernard Saucier