【发布时间】:2021-08-18 04:33:41
【问题描述】:
我正在学习 node.js 并致力于一个项目,该项目旨在创建一个 Web 应用程序来为投资者管理股票市场投资组合。我不知道如何将捕获的数据从 API 发送到 html 中的表格单元格。 这是我的 node.js 代码
node.js file
const express = require("express");
const https = require("https");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({extented:true});
app.get ("/", function(req,res){
//The stock market API
const url= "https://api.{stock-market-api}"
//https get request to API
https.get(url, function(response){
console.log(response.statusCode);
response.on("data", function(data){
//converting the API jason data to javascript object
const stockDataObject = JSON.parse(data);
//capturing the data I want to a const name companyPrice
const companyPrice = stockDataObject.data.comapny1.IN3wmk;
})
})
});
app.listen(3000, function(){
console.log("sever is running");
})
html file
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>document</title>
<style media="screen">
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Market portfolio</h1>
<table>
<thead>
<tr>
<th>table head1</th>
<th>table head2</th>
<th>table head3</th>
<th>table head4</th>
</tr>
</thead>
<tbody>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
<td>data4</td>
</tr>
</tbody>
</table>
</body>
</html>
如何将我从“const comapnyPrice”捕获的数据发送到 html 表中 td 元素中的 data4 单元格。请帮助我,因为我是编码的初学者。非常感谢
【问题讨论】:
-
你如何提供你的 html 文件?
-
取而代之的是,您为什么不使用模板文字来呈现整个表格。它是一种选择。
标签: javascript html node.js api web-applications