【问题标题】:How to send data from Node.js server to HTML document?如何将数据从 Node.js 服务器发送到 HTML 文档?
【发布时间】:2020-09-03 11:19:57
【问题描述】:

我想将从 API 获取的数据从 Node.js 上的服务器发送到 HTML 页面。 下面是我的 Node.js 代码;

const express = require("express");

const app = express();

const https = require("https");
const bodyparser = require("body-parser");


app.use(express.static("public"));
app.use(bodyparser.urlencoded({
  extended: true
}));



//getting request from browers so sending response
app.get("/", function(req, res) {
  res.sendFile(__dirname + "/index.html");
});


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

  const query = req.body.query;
  const appid = "//appid";
  const units = "metric";
  const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=" + appid + "&units=" + units

  https.get(url, function(respond) {

    console.log(respond.statusCode);
    respond.on("data", function(data) {


      const weatherdata = JSON.parse(data)
      console.log(weatherdata);
      const tempe = weatherdata.main.temp;
      const des = weatherdata.weather[0].description;


        res.sendFile(__dirname+"/output");


    });
  });
});




app.listen(8085, function() {

  console.log("server started at port 3000");

});


这是我要向其发送数据的 output.html 的代码


    <!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Weather Report</title>
    <link rel="stylesheet" href="css/styles.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

        <style>
          .bd-placeholder-img {
            font-size: 1.125rem;
            text-anchor: middle;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
          }

          @media (min-width: 768px) {
            .bd-placeholder-img-lg {
              font-size: 3.5rem;
            }
          }
        </style>



  </head>
  <body class="text-center">
    <section class=" form-signin">
      <div class="headingdiv">
           <form class="form" action="/output" method="get">
              <h1 id="Temperature" name="tem">Tempe here</h1>
                  <p class="mt-5 mb-3 text-muted">&copy; 2020</p>

           </form>
      </div>
    </section>

  </body>
</html>


我使用了一个发送数据的 Weather API,我想将该数据显示到一个新的 HTML 文档中。

【问题讨论】:

  • 你不能“发送”,客户端需要请求它。
  • @jonrsharpe 嗨,用户来自 bowser 的请求,当我收到请求时,我已经发送了 index.html 的主页,将数据馈送到 index.html 并发送到 API 请求和现在收到响应我想将该响应发送到输出 .HTML 作为对用户请求的反馈
  • 这不是聊天;请使用tour 并查看How to Ask

标签: html node.js dom


【解决方案1】:

您需要使用ejs或jade模板系统将数据填充到html文件中,然后将其发送给用户作为响应。 供您参考,这里有一个link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2021-09-05
    • 2019-03-11
    • 2012-04-03
    • 2013-10-17
    • 2023-03-08
    相关资源
    最近更新 更多