I'm configuring a very basic server with node(without express). I have a html file which has a link to javascript file. Right now I'm able to load only the html file and not the js resource to it. how to load the js file?
server.js
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var scriptFile = path.join(__dirname), '/script.js')
http.createServer(function(req, res) {
if (req.url === '/') {
fs.readFile('./index.html', function(err, data) {
if (err){
throw err;
}
res.writeHead(200, {"Content-Type": 'text/html'});
res.write(data);
fs.readFileSync(path.normalize(scriptFile)) //throws error here. script file when html page is loaded.
//i'm trying to load the script.js when html file is loaded
res.end();
return;
});
}
}).listen(3000);
html:
<body>
<h1> Hi </h1>
<script src="script.js">
</body>