Q:

Running an html response without opening the browser

I'm trying, using node.js (request package), to get a server response of html file, adding a script to it and run this file with the script.
The node.js:
const request= require ('request')
const fs = require  ('fs')
request.get ('www.google.com', (err, data, body) => {
if (err) throw err
body+='<script src="script.js"></script>'
fs.writeFile ('h.html', body)
})

The script.js:
window.onLoad = () =>alert ('something')

How can I do it without saving the response in html file and open it in the browser?

A:

You can do the following:
var templateHtml = "<!DOCTYPE html><html><head></head><body>Your HTML</body></html>";
app.get('/your_resource',function(req,res){
    res.send(templateHtml);
});

Of course, you need to replace in templateHtml the script.
Take a look at this answer to get more info about how to use HTML templates.
For example, imagine you have the package express-es6-template-engine in your project.
NodeJs
var express = require('express'),
    es6Renderer = require('express-es6-template-engine'),
    app = express();

app.engine('html', es6Renderer);
app.set('views', 'views');
app.set('view engine', 'html');

app.get('/', function (req, res) {
    res.render('index', {locals: {title: 'Welcome!', script: '<script>//script.js</script>'}});
});

app.listen(3000);

Html
<!DOCTYPE html>
<html>
<head>
    ${script}
</head>
<body>
<h1>${title}</h1>
</body>
</html>

Hope it helps!

