0

I am new to JavaScript . I wants to run a html page index.html but could not. how can html be not defined. please help. the code here just creates a server and runs a html page.

  var http=require('http');
      var fs=require('fs');
      fs.readFile('index.html',(err,html)=>
      {if (err) throw err;})
       var server=http.createServer((req,res)=>{`enter code here`
       res.statusCode=200;
       res.setHeader('contentType','text/html');
       res.write(html);
       //res.writeHead(200,{'Content-Type':'text/html'});
       res.end();
      }).listen(8081);

          console.log('server started');
2
  • Do you want to serve an html ? Commented Apr 17, 2018 at 15:44
  • yeh, it was a typo .corrected it now. thanks. Commented Apr 18, 2018 at 2:37

1 Answer 1

1
var http = require('http');
var fs = require('fs');
fs.readFile('index.html', (err,html) => {
    if (err) { throw err; }
    var server = http.createServer((req,res) => {
        res.statusCode=200;
        res.setHeader('contentType', 'text/html');
        res.write(html);
        res.end();
    }).listen(8081);
    console.log('server started');
});

That's why you should always indent, format and read your own code (and maybe consider using linter because it would have told you exactly what was wrong) because I'm not doing your job next time.

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

1 Comment

thank-you. I will be more careful next time. please can you also suggest good books and any tutorial for the same. I am a beginner and any help will be appreciated.

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.