简体   繁体   English

如何将数据作为响应从 nodejs 服务器发送到 html 页面,以便以表格格式显示

[英]How to send data as a response from nodejs server to html page so display it in table format

I'm new to programming and I'm building a project which is a web app using nodejs/express js server.我是编程新手,正在构建一个项目,该项目是使用 nodejs/express js 服务器的 Web 应用程序。 I'm stuck at making a table with my response that I get back from server.我被困在用我从服务器返回的响应制作表格。

  1. On app.get('/') I go to homepage and do some task and click submit button that post the data to serverapp.get('/')我转到主页并执行一些任务,然后单击将数据发布到服务器的提交按钮
  2. After some processing on server side I get an array of object as a result that is stored in variable result=[{},{},{}]在服务器端进行一些处理后,我得到一个存储在变量result=[{},{},{}]的对象数组
  3. I can see this result when I return res.json({staus:200,values:result}) from当我返回res.json({staus:200,values:result})时,我可以看到这个结果
app.post('/result',< handler fuction >()=>{ // perform some task and get result into result variable res.json({staus:200,values:result}) })
  1. but I want to use this result to build html table based on length of array (it may be different every time) and send that html file with table as a response to browser.但我想使用这个结果来构建基于数组长度的 html 表(每次都可能不同)并将该 html 文件与表一起发送给浏览器。

I tried to create a basic html file and return this file in res.send('table.html') that was loading a in an external javascript file and this file tries to append table.html to create table.我试图创建一个基本的 html 文件并在res.send('table.html')中返回这个文件,该文件正在加载一个外部 javascript 文件,这个文件试图附加 table.html 来创建表。 But I don't know how I can send result var from server to this file that works on client side.但我不知道如何将result变量从服务器发送到这个在客户端工作的文件。

您可以使用像 (ejs) 或任何其他模板引擎这样的模板引擎来显示从服务器返回的动态数据,如果您想在不同的页面中呈现结果,您应该将数据存储在任何类型的存储中像本地存储或数据库一样能够在不同的地方使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM