I was studying nodejs peacefully. and I was learning about fs.readFile. So for practice, I made a simple code.
var list;
fs.readFile('list.txt', 'utf8', function(err, data){
list=data;
});
but when I got the value of list, it was "undefined".
So, I tried to console data's value like this
var list;
fs.readFile('list.txt', 'utf8', function(err, data){
list=data;
console.log(data);
});
the problem start's now.
I got the error,
TypeError: console.log is not a function.
So, I checked on many questions like this or this...
But I could not find the answer to my question.
Why is this error happening, and how can I stop this happening?
the full code is
var http = require('http');
var fs = require('fs');
var url = require('url');
var printconsole="";
function console(data){
return "<script>console.log(\""+data+"\");</script>";
}
var app = http.createServer(function(request,response){
var template;
var _url = request.url;
var queryData = url.parse(_url, true).query;
var title = queryData.id;
var main;
var list;
fs.readFile('list.txt', 'utf8', function(err, data){
console.log("data");
list=data;
});
fs.readFile(`${queryData.id}.txt`, 'utf8', function(err, data){
main=data;
});
if(_url == '/'){
title = 'Web';
main = `world wide web`;
}
if(_url == '/favicon.ico'){
return response.writeHead(404);
}
response.writeHead(200);
template = `
<!doctype html>
<html>
<head>
<style>
body{
width:-webkit-fill-availble;
height:-webkit-fill-availble;
}
header{
width:100%;
height:10%;
border-bottom:1px solid #111111;
}
aside{
width:10%;
height:560px;
border-right:1px solid #111111;
float:left;
}
article{
width:85%;
height:85%;
}
</style>
<meta charset="utf-8">
</head>
<body>
<header><h1>${title}</h1></header>
<aside>${list}</aside>
<article>${main}</article>
</body>
</html>
`;
response.end(template);
});
app.listen(3000);