向客户端发送已解析的 JSON 文件但格式不是 HTML

sending parsed JSON file to client but format is not HTML

调用了 API,并解析了 JSON。现在想在接收页面中以 table 格式显示解析的 JSOn 。它以纯文本形式出现。 API url 只是为了测试目的从 WIKI 中获取,不能 post 实际 API 出于安全目的。

要求是以table格式发送HTMl文件作为输出。

app.js 文件:

    const express = require("express");
    const bodyParser = require("body-parser");
    const https = require("https");
    var fs = require('fs');
    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 str1 = str.replace(/\n/g, '');
    const jsdom = require("jsdom");
    const {
      JSDOM
    } = jsdom;

    const virtualConsole = new jsdom.VirtualConsole();

    const dom = new JSDOM(str1, {
      virtualConsole,
      runScripts: "dangerously",
      resources: "usable"
    });
    var strWrite = dom.window.document.querySelector("table").textContent;
    res.setHeader("Content-Type", "text/html");
    res.write(strWrite);
    res.send();
      });
     });
       });
    });
    app.listen(3000, function(req, res) {
     console.log("server is running in port 3000");
    });

index.html 文件:

    <!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>
      
    </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="app.js" charset="utf-8"></script>
    </body>

    </html>

我收到的输出: 明文格式:

Anthony Martial2017年效力曼联个人信息全名Anthony Jordan Martial[1]出生日期 (1995-12-05) 1995 年 12 月 5 日(24 岁)[2]出生地 Massy, 法国身高1.81米(5英尺11英寸)[3]上场位置前锋俱乐部信息当前 球队曼联号码9青年生涯2001–2009CO Les Ulis2009–2012里昂成年 职业年TeamApps(Gls)2012–2013Lyon B11(5)2013Lyon3(0)2013Monaco B4(3)2013–2015摩纳哥49(11)2015–曼联143(51)国家队–2010–2011法国 U1617(9)2011–2012法国 U1713(9)2012–2013法国 U184(3)2013法国 U195(0)2013–2015法国 U2112(4)2015–France18(1) 荣誉代表法国男子足球欧足联 冠军亚军2016年法国 成年俱乐部出场次数和入球数为国内 仅限联赛,截至 2020 年 7 月 16 日(UTC)21:16 正确 - 国家队出场次数和进球正确 截至 2018 年 3 月 27 日

问题已解决。

选项1: 使用解析 JSON 转义字符的 repalce 语句已从字符串中删除。 现在完全以 table 格式显示它。

选项2: 连接并标记然后在末尾关闭标记。 然后它以 table.

的形式出现