【问题标题】:Uncaught Syntax Error: Unexpected token (First program)未捕获的语法错误:意外的令牌(第一个程序)
【发布时间】:2019-06-24 02:34:29
【问题描述】:

Uncaught SyntaxError: Unexpected token

我正在编写我在 node 中的第一步,一开始没有任何框架来理解 node.js 的主要和简单方面

我刚刚在 Node.js (main.js) 中创建了我的服务器并调用了一个 index.html,这个 index.html 调用了一个 sayHi.js,它只有一个控制台日志。但是这个脚本不起作用...

我想对这个 HTML 文件做一个控制器/脚本来开始编程...

//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

【问题讨论】:

  • 您的服务器不提供任何 .js 文件。
  • &lt;a href="\modbus"&gt;Modbus&lt;/a&gt; 将是 &lt;a href="/modbus"&gt;Modbus&lt;/a&gt;
  • 您的 HTML 文件底部有一个错误的 &lt;/script&gt; 标签,但不确定它的相关性。
  • 读取错误的行号。
  • 当您的浏览器遇到脚本标签时,它会向您的服务器询问 js 文件,而您的服务器正在响应一个 html 页面(可能是您的 404 页面),因为您没有为js 文件。

标签: javascript html node.js


【解决方案1】:

非常感谢您的帮助:

这句话帮助了我:我的建议是在标头中返回适当的状态码,即 404,并在您的服务器文件中构建到 sayHi.js 的路由。

Solution

【讨论】:

    【解决方案2】:
     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

    我该怎么做? 我的建议是在标头中返回适当的状态代码,即 404,并在您的服务器文件中构建到 sayHi.js 的路由。

    【讨论】:

    • res.writeHead(404, { 'Content-Type': 'text/html' });
    【解决方案3】:

    我要指出的第一件事是,您的 HTML 文件正在从 ../scripts/sayHi.js 路径中查找名为 sayHi.js 的 javascript 文件。

    您的 Html 文件在 http://localhost:8080/ 提供,并尝试获取您没有路由到的 http://localhost:8080/scripts/sayHi.js,因此 server.js 将尝试发送错误 html,它位于 404.html 文件中。

    这是一个 HTML 文件,但作为 javascript 文件注入,这将导致浏览器中的控制台错误。

    我的建议是在标头中返回适当的状态代码,即 404,并在您的服务器文件中构建到 sayHi.js 的路由。

    【讨论】:

      【解决方案4】:

      删除html标签&lt;/html&gt;上方的脚本标签&lt;/script&gt;

      从这里更改以下代码:

          <!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>
      

      到这里:

          <!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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 2014-02-09
        • 2011-04-07
        • 2014-05-09
        • 2014-11-24
        • 2012-09-07
        • 2015-03-21
        相关资源
        最近更新 更多