Node JS - Getting 'Unexpected token <'

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.');

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) {

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

The index.html file:

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

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?

1 Like