For training purposes, I am trying to display a portion of the pathname in my html page.
For instance, if URL is like : http://localhost:8080/firstname/abcd
I expect my browser to display: "Hello abcd"
const config = require('./config');
var http = require('http');
var url = require('url');
var server = http.createServer(function(req, res) {
var path = url.parse(req.url).pathname;
if(path.includes('firstname'))
{
firstname = path.replace('/firstname/', '');
res.writeHead(200, {"Content-Type": "text/html"});
res.write('<!DOCTYPE html>'+
'<html>'+
' <head>'+
' <meta charset="utf-8" />'+
' <title>Node.js tests</title>'+
' </head>'+
' <body>'+
' <p id = "name"></p>'+
' </body>'+
'<script type="text/javascript">
'document.getElementsById("name").innerHTML = ("Hello " + firstname)'+
'</script>'+
'</html>');
res.end();
}
else
{
console.log('Nothing to display');
}
});
server.listen(config.env.port);
As you can guess, the page appears empty.