U
U
Umid2016-12-04 17:21:34
JavaScript
Umid, 2016-12-04 17:21:34

Throws Uncaught SyntaxError: Unexpected identifier?

Displays the error client.js:1 Uncaught SyntaxError: Unexpected identifier in the browser console.
It doesn't matter if there is code inside or not.
I climbed a little and found out that the whole thing is in the server.
Scripts:

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

http.createServer(function(req, res) {
  if(req.url == "/") {
    fs.readFile("index.html", function(err, content) {
      if(err) {
        console.log("error");
        return;
      }
      res.end(content);
    })
  } else {
    res.end("Bad request");
  }
}).listen(3000);

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Node.JS</title>
</head>
<body align="center">
  <h1>Hello man!</h1>
  <textarea id="theText" placeholder="Input the Text" cols="45" rows="10"></textarea>
  <br>
  <input type="button" id="sendText" value="Send Me">

  <script type="text/javascript" src="client.js"></script>

</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yustas Alexu, 2016-12-04
@DarCKoder

Try like this:

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

http.createServer(function(req, res) {
  if(req.url == "/") {
    fs.readFile("index.html", {encoding: 'utf8'}, function(err, content) {
      if(err) throw err;
      res.end(content);
    });
  } else if(req.url.includes('client')) {
    fs.readFile("client.js", {encoding: 'utf8'}, function(err, content) {
      if(err) throw err;
      res.end(content);
    });    
  } else {
    res.end("Bad request");
  }
}).listen(3000);

And make sure index.html is in the same encoding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question