Assume we have an html file that contains DOM manipulation scripts:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
let el = document.createElement('div');
let text = document.createTextNode('Hello World!');
el.appendChild(text);
document.body.appendChild(el);
</script>
</body>
</html>
When this file is sent to the browser, it gets rendered as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
let el = document.createElement('div');
let text = document.createTextNode('Hello World!');
el.appendChild(text);
document.body.appendChild(el);
</script>
<div>Hello World!</div>
</body>
</html>
Assuming the source html is sent to an agent other than the browser, we want the rendered html to appear to this agent. Obviously the agent does not contain a javascript interpreter to render the DOM operations. So we need the server to render the javascript in the script tag before sending it to the agent. What is the tool that we can use in nodejs to render the html before sending them to the agent?