Node JS - Getting 'Unexpected token <'

Node JS - Getting 'Unexpected token <'
0

#1

I am trying to build a simple server in NodeJS. Here are the files.

First, the server file:

var http = require('http');
var fs = require('fs');
var path = require('path');

function send404(response) {
    response.writeHead(404, { 'Content-Type': 'text/plain' });
    response.write('Error 404: Resource nbmnb not found.');
    response.end();
}

var mimeLookup = {
    '.js': 'application/javascript',
    '.html': 'text/html'
};

var server = http.createServer(function (req, res) {
	if (req.method == 'GET') {
		// resolve file path to filesystem path
		var fileurl;
		if (req.url == '/') {
			fileurl = '/index.html';
		}
        else {fileurl = req.url;}
        var filepath = path.resolve('./' + fileurl);
		
		// lookup mime type
        var fileExt = path.extname(filepath);
        var mimeType = mimeLookup[fileExt];
        if (!mimeType) {
            send404(res);
            return;
        }

        // see if we have that file
        fs.exists(filepath, function (exists) {
			
			// if not
            if (!exists) {
				send404(res);
                return;
            };
			
        // finally stream the file
        res.writeHead(200, { 'content-type': 'text/html' });
        fs.createReadStream('./index.html').pipe(res);
		});
    }
    else {
        send404(res);
    }
}).listen(3000);

The index.html file:

<html>
 <head>
  <title>Hello there</title>
  <script src='./main.js'></script>
 </head>
 <body>    You are looking lovely!</body>
</html>

and the main.js file, which is loaded when index.html is loaded:

window.onload = function () {
    document.body.innerHTML += '<strong>Talk JavaScript with me</strong>';
}

The index.html loads on the page, but the text in main.js does not load. Instead I am getting Uncaught SyntaxError: Unexpected token < in the console, error locate in the first position of main.js. From what I understand, this usually happens when the server returns a text/html response and expects something else in return. Any ideas?