The code in the question is not doing what you think it is doing, because:
jsdom
won't modify your./index.html
file.Even if
jsdom
did modify your file,jsdom.env
is asynchronous so you'd still be sending the file from before the change.
What you could do is change your code to send a serialized version of the DOM you modified. The following code changes the text and serves it to the client:
env('./index.html', function (errors, window) {
var $ = require('jquery')(window);
$('#header').html('new-text');
res.write($("html")[0].outerHTML);
res.end();
});
Note that the code above is meant to illustrate what a solution could look like. It is not meant to be used as-is in production.
A final note: using jsdom and jQuery to perform the task you are doing seems extremely "heavy" to me. A templating system which is designed for generating templates on the fly for the purpose of producing HTTP responses would most likely be faster than using jsdom + jQuery.