Disclaimer: I am new to node.js so I assume there is a very basic answer to this question.
I am using node.js on Windows, with http module to generate a static page with content generate from a js file.
Server file (server.js):
var http = require('http');
var fs = require('fs');
fs.readFile('./index.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8080);
});
HTML file (index.html):
<!DOCTYPE html>
<html>
<body>
<div id="div1">
<p id="p1">This is a static paragraph.</p>
</div>
<p>
<script type="text/javascript" src="start.js"></script>
</p>
</body>
</html>
JS file (start.js)
var para = document.createElement("p");
var node = document.createTextNode("This text is loaded from a js file.");
para.appendChild(node);
var element = document.getElementById("div1");
element.appendChild(para);
When I load index.html directly, I get the following output:
This is a static paragraph.
This text is loaded from a js file.
^^ this is my desired output.
Problem: However, when I use node.js and run server.js, the start.js content does not load. I get:
This is a static paragraph.
Any help would be appreciated.
FYI, my folder structure
/
- node_modules
-- http
-- fs
- index.html
- server.js
- start.js