Reputation: 3
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');
Upvotes: 0
Views: 253
Reputation: 961
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.
Upvotes: 1