Shourya Sharma
Shourya Sharma

Reputation: 13

Giving error while creating server using node js

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' }

Upvotes: 0

Views: 1627

Answers (3)

OMKAR LINGVAT
OMKAR LINGVAT

Reputation: 1

Please enter the folder name before the html file name:

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

Upvotes: 0

Lakshaya U.
Lakshaya U.

Reputation: 1171

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.

Upvotes: 1

vishal patil
vishal patil

Reputation: 71

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}`); });

Upvotes: 0

Related Questions