1

I created this javascript file , index.html , about.html , contact.html , services.html in a same folder (tut67) but still it is giving error for each html file . (I use MacBook Air).

Code is given below :::

const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';

const port = 3000;

const home = fs.readFileSync('index.html');

const about = fs.readFileSync('./about.html');

const contact = fs.readFileSync('./contact.html');

const services = fs.readFileSync('./services.html');

const server = http.createServer((req,res)=>{
    console.log(req.url);

    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    res.end(home);
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Error is given below :::

internal/fs/utils.js:307
    throw err;
    ^

Error: ENOENT: no such file or directory, open 'index.html'

    at Object.openSync (fs.js:476:3)
    at Object.readFileSync (fs.js:377:35)
    at Object.<anonymous> (/Users/shouryasharma/Desktop/Web Dev/tut67/index.js:7:17)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47 {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: 'index.html'
}

4 Answers 4

1

Node doesn't understand where your files exactly are, even though they are in the same directory. Assuming this is your directory structure:

tut67
     | index.html
     | about.html
     | contact.html
     | services.html
     | app.js

This would be the contents of app.js:

const http = require('http');
const fs = require('fs');
const path = require('path');

const hostname = '127.0.0.1';
const port = 3000;

const home = fs.readFileSync(path.join(__dirname, 'index.html')).toString();
// const about = fs.readFileSync(path.join(__dirname, 'about.html')).toString();
// const contact = fs.readFileSync(path.join(__dirname, 'contact.html')).toString();
// const services = fs.readFileSync(path.join(__dirname, 'services.html')).toString();

// create the http server
const server = http.createServer((req,res) => {
    console.log(req.url);
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    res.end(home);
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

Sample contents for index.html could be:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello World</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html> 

This would render out index.html and log the request url to the console appropriately. Keep in mind the unused variables can be (and should be) commented out.

Sign up to request clarification or add additional context in comments.

Comments

1

You should use __dirname (a constant that define the absolute path to the folder where is the current script):

const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';

const port = 3000;

const home = fs.readFileSync(__dirname + '/index.html');

const about = fs.readFileSync(__dirname + '/about.html');

const contact = fs.readFileSync(__dirname + '/contact.html');

const services = fs.readFileSync(__dirname + '/services.html');

const server = http.createServer((req,res)=>{
    console.log(req.url);

    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/html');
    res.end(home);
});

server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);
});

With this method, the fs.readFileSync() method will know exactly where the file is located. The only thing I don't understand is why fs throws an error. On my PC it doesn't, but I think it's because I'm using Windows and you're using macOS.

Comments

0

I am not able to reproduce your error, You can achieve your goal like this also.

const http = require('http'); const fs = require('fs');

const hostname = '127.0.0.1';

const port = 3000;

const home = fs.readFileSync('index.html');


const server = http.createServer((req,res)=>{ console.log(req.url);

res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
fs.createReadStream('index.html').pipe(res)
;
});

server.listen(port, hostname, () => { console.log(`Server running at http:${hostname}:${port}`); });

Comments

0

Please enter the folder name before the html file name:

const home = fs.readFileSync('introbackend/home.html');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.