praveen praveen - 3 months ago 21
Node.js Question

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:


Answer

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

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