How to include javascript on client side of node.js?
javascript, node.js
Solution
The problem is that nomatter what your browser requests, you return "index.html". So the browser loads your page and get's html. That html includes your script tag, and the browser goes asking node for the script-file. However, your handler is set up to ignore what the request is for, so it just returns the html once more.
Problem
I'm a beginner of node.js and javascript. I want to include external javascript file in html code. Here is the html code, "index.html": ``` <script src="simple.js"></script> ``` And, here is the javascript code, "simple.js": ``` document.write('Hello'); ``` When I open the "index.html" directly on a web browser(e.g. Google Chrome), It works. ("Hello" message should be displayed on the screen.) However, when I tried to open the "index.html" via node.js http server, It doesn't work. Here is the node.js file, "app.js": ``` var app = require('http').createServer(handler) , fs = require('fs') app.listen(8000); function handler (req, res) { fs.readFile(__dirname + '/index.html', function (err, data) { if (err) { res.writeHead(500); return res.end('Error loading index.html'); } res.writeHead(200); res.end(data); }); } ``` ("index.html", "simple.js" and "app.js" are on same directory.) I started the http server. (by "bash$node app.js") After then, I tried to connect "localhost:8000". But, "Hello" message doesn't appear. I think the "index.html" failed to include the "simple.js" on the http server. How should I do?