dom-即使从 NPM 安装后,解析器也会抛出语法错误

dom-parser throwing syntax error even after installed from NPM

我是这项技术的初学者。请指导。 从 NPM 软件包安装 dom-parser。然后当我在下面声明时它抛出 syntax 错误。 我知道这是一件非常小的事情,但我很困惑,不明白我哪里出错了。 已经执行 npm install dom-parser.

代码如下:

    const DOMParser = require('dom-parser'); 
    var domParser = new DOMParser();​​

错误信息如下: var domParser = new DOMParser();​​

    SyntaxError: Invalid or unexpected token
        at wrapSafe (internal/modules/cjs/loader.js:1054:16)
        at Module._compile (internal/modules/cjs/loader.js:1102:27)
        at Object.Module._extensions..js 
        (internal/modules/cjs/loader.js:1158:10)
        at Module.load (internal/modules/cjs/loader.js:986:32)
        at Function.Module._load (internal/modules/cjs/loader.js:879:14)
        at Function.executeUserEntryPoint [as runMain] 
        (internal/modules/run_main.js:71:12)
        at internal/main/run_main_module.js:17:47
        [nodemon] app crashed - waiting for file changes before starting...

谢谢, 乔瓦

应专家要求, 下面的代码片段:

app.js 文件:

    const express = require("express");
    const bodyParser = require("body-parser");
const https = require("https");
const DOMParser = require('dom-parser');
const app = express();
app.use(bodyParser.urlencoded({
  extended: true
}));


app.get("/", function(req, res) {

  res.sendFile(__dirname + "/index.html");
});

app.post("/", function(req, res) {


  const url = "https://en.wikipedia.org/w/api.php?format=json&action=parse&page=Anthony%20Martial";

  https.get(url, function(response) {

    https.get(url, (resp) => {
      let data = '';

      // A chunk of data has been recieved.
      resp.on('data', (chunk) => {
        data += chunk;
      });

      // The whole response has been received. Print out the result.
      resp.on('end', () => {
        const jsonDATA = JSON.parse(data).parse;
        var jsonText = jsonDATA.text;
        var jsonTitle = jsonDATA.title;
        var jsonPageid = jsonDATA.pageid

        var str = JSON.stringify(jsonText);


        var domParser = new DOMParser();​​



        res.write("<p>player title is " + jsonTitle + " degree C</p>");
        res.write("<p>The pageid is " + jsonPageid + "</p>");
        res.write("<p>The pagetext is is " + str + "</p>");
        res.send();

      });
    });
  });
});



app.listen(3000, function(req, res) {
  console.log("server is running in port 3000");
});

index.html 文件: 请记住 index.html 的输入字段与 js 文件没有任何联系。这只是为了测试目的。

    <!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>weatehr App API</title>

  <!-- <script src="app.js" charset="utf-8"></script> -->

  <link rel="stylesheet" href="css/style1.css">
  <link rel="stylesheet" href="css/style2.css">
</head>

<body>

  <form action="/" method="post">
    <label for="cityInput">City Name:</label>
    <input id="cityInput" type="text" name="cityName">
    <button type="submit"> Go </button>
    <h3>hello</h3>
  </form>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


 <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"    crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"  crossorigin="anonymous"></script>
  <script src="app.js" charset="utf-8"></script>
</body>

</html>

JSon 文件:安装包。

{
  "name": "callwikiapi-2",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "dom-parser": "^0.1.6",
    "express": "^4.17.1"
  }
}

使用codesandbox也有同样的错误。 请 heko ,我哪里错了。有没有我必须安装的依赖包?

附加 PFA 代码和框。

Issur 使用 jsdom 解决。

    const jsdom = require("jsdom");
    const { JSDOM } = jsdom;
    const dom = new JSDOM("<!DOCTYPE html><p>Hello world</p>", { runScripts: "dangerously" });

    console.log(dom.window.document.querySelector("p").textContent);

感谢专家抽出宝贵时间。