Miguel-Ángel
Miguel-Ángel

Reputation: 57

Uncaught Syntax Error: Unexpected token (First program)

Uncaught SyntaxError: Unexpected token < Calling a javascript with console.log ("hello world");

I'm codding my first steps in the node, without any framework at the beginning to understand the main and simple aspects of node.js

I just created my server in Node.js (main.js) and calling an index.html, this index.html call a sayHi.js, it only has a console log. But this script not works...

I would like to do a controller/script to this HTML file to start programming...

//main.js
var http = require("http");
var fs = require("fs");
var url = require('url');

//create a server object:
http.createServer(function(req, res) {

    if (req.url === "/index" || req.url === '/') {
      fs.readFile('app/views/index.html', function(err, data) {
        res.writeHead(200, {
          'Content-Type': 'text/html'
        });
        console.log("Requested URL is index: " + req.url);
        res.write(data);
        res.end();
      });
    } else if (req.url === "/modbus") {
      fs.readFile('app/views/modbus.html', function(err, data) {
        res.writeHead(200, {
          'Content-Type': 'text/html'
        });
        console.log("Requested URL is index: " + req.url);
        res.write(data);
        res.end();
      });

    } else {
      fs.readFile('app/views/404.html', function(err, data) {
        res.writeHead(200, {
          'Content-Type': 'text/html'
        });
        console.log("Requested URL is index: " + req.url);
        res.write(data);
        res.end();
      });
    }
  })
  .listen(8080); //the server object listens on port 8080
console.log('Server running at http://127.0.0.1:8080');
<!DOCTYPE html>
<html>
  <head>
    <title>Index</title>
    <script src="../scripts/sayHi.js"></script>
    <!-- <link rel="stylesheet" href="/app/styles/myStyle.css" /> -->
  </head>

  <body>
    <h1>Hello! Now you know how to serve HTML files using Node.js!!!</h1>

    <p id="demo"></p>
    <a href="\modbus">Modbus</a>
  </body>

</script>
</html>
//sayHi.js
console.log("hello world");

Uncaught SyntaxError: Unexpected token <

Upvotes: 0

Views: 4973

Answers (4)

Miguel-&#193;ngel
Miguel-&#193;ngel

Reputation: 57

Thank you so much for your help:

This sentence helped me: My suggestion is to return appropriate status code, i.e. 404, in the header, and build a route to the sayHi.js in your server file.

Solution

Upvotes: 0

Miguel-&#193;ngel
Miguel-&#193;ngel

Reputation: 57

 else if(req.url === "/sayHi"){
      fs.readFile('/app/scripts/sayHi.js', function (err, data) {
        res.writeHead(200, {
          'Content-Type': 'text/plain'
        });
        console.log("Requested URL is index: " + req.url);
        res.write(data);
        res.end();
      });

@reza

How can I do this? My suggestion is to return appropriate status code, i.e. 404, in the header, and build a route to the sayHi.js in your server file.

Upvotes: 0

Reza
Reza

Reputation: 867

The first thing I would point out is that your HTML file is looking for a javascript file under the name of sayHi.js, from ../scripts/sayHi.js path.

Your Html file is served at http://localhost:8080/ and tries to get http://localhost:8080/scripts/sayHi.js to which you have no route, so the server.js will try to send the error html, which is in 404.html file.

this is an HTML file, but is injected as a javascript file, which will result in the console error in the browser.

My suggestion is to return appropriate status code, i.e. 404, in the header, and build a route to the sayHi.js in your server file.

Upvotes: 2

Faeza
Faeza

Reputation: 101

Remove the script tag </script> above html tag </html>.

Change the following code from this:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Index</title>
        <script src="../scripts/sayHi.js"></script>
        <!-- <link rel="stylesheet" href="/app/styles/myStyle.css" /> -->
      </head>

      <body>
        <h1>Hello! Now you know how to serve HTML files using Node.js!!!</h1>

        <p id="demo"></p>
        <a href="\modbus">Modbus</a>
      </body>

    </script>
    </html>

To this:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Index</title>
        <script src="../scripts/sayHi.js"></script>
        <!-- <link rel="stylesheet" href="/app/styles/myStyle.css" /> -->
      </head>

      <body>
        <h1>Hello! Now you know how to serve HTML files using Node.js!!!</h1>

        <p id="demo"></p>
        <a href="\modbus">Modbus</a>
      </body>

    </html>

Upvotes: 0

Related Questions