How to include javascript on client side of node.js? -
i'm beginner of node.js , javascript.
i want include external javascript file in html code. here html code, "index.html":
<script src="simple.js"></script>
and, here javascript code, "simple.js":
document.write('hello');
when open "index.html" directly on web browser(e.g. google chrome), works. ("hello" message should displayed on screen.)
however, when tried open "index.html" via node.js http server, doesn't work. here 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" , "app.js" on same directory.) started http server. (by "bash$node app.js") after then, tried connect "localhost:8000". but, "hello" message doesn't appear.
i think "index.html" failed include "simple.js" on http server.
how should do?
the problem nomatter browser requests, return "index.html". browser loads page , get's html. html includes script tag, , browser goes asking node script-file. however, handler set ignore request for, returns html once more.
Comments
Post a Comment