I didn't write hyperlink in my body code. However, it get hyperlink linked to last data list('Javascript'). Why <h2>${title}</h2>${description} get hyperlink in my code?
var http = require('http');
var fs = require('fs');
var url = require('url');
function templateHTML(title, list, body){
return `
<!doctype html>
<html>
<head>
<title>WEB1 -${title}</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="/">WEB</a></h1>
${list}
${body}
</body>
</html>
`;
}
function templateList(filelist){
var list = '<ul>';
var i = 0;
while(i < filelist.length){
list = list + `<li><a href="/?id=${filelist[i]}">${filelist[i]}</li>`;
i += 1;
}
list = list +'</ul>';
return list;
}
var app = http.createServer(function(request,response){
var _url = request.url;
var queryData = new URL('http://localhost:3000' + _url).searchParams;
var pathname = new URL('http://localhost:3000' + _url).pathname;
if(pathname === '/'){
if(queryData.get('id') === null){
fs.readdir('./data', function(error, filelist){
//console.log(filelist);
var title = 'welcome';
var description = 'Hi there';
var list = templateList(filelist);
var template = templateHTML(title, list, `<h2>${title}</h2>${description}`);
response.writeHead(200);
response.end(template);
})
} else {
fs.readdir('./data', function(error, filelist){
//console.log(filelist);
fs.readFile(`data/${queryData.get('id')}`, 'utf8', function(err, description){
var title = queryData.get('id');
var list = templateList(filelist);
var template = templateHTML(title, list, `<h2>${title}</h2>${description}`);
response.writeHead(200);
response.end(template);
});
});
}
} else {
response.writeHead(404);
response.end('Not found');
};
});
app.listen(3000);