使用Node.js和Express将JSON数据集显示为表 [英] Displaying a JSON dataset as a table with Node.js and Express

查看:72
本文介绍了使用Node.js和Express将JSON数据集显示为表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想指出我对Node.JS很新。我正在尝试使用NodeJS创建一个包含多个表和信息的页面。我的问题是,我无法将SQL查询的结果导入HTML表格。目前我 .send 使用express将数据导入HTML页面。

First of all, I would like to point out that I'm very new to Node.JS. I'm trying to use NodeJS to make a page containing multiple tables and info. My problem is, that I can't get the result from the SQL query into an HTML table. Currently I .send the data into an HTML page using express.

我使用的代码:

    var http = require('http');
    http.createServer(function(req, res) {});
      var mysql = require("mysql"); 
      var express = require('express');
      var app = express();
      console.log('Creating the http server');
      con.query('SELECT id ,name FROM customer', function(err, rows, fields)
    {
        console.log('Connection result error '+err);
        console.log('num of records is '+rows.length);

        app.get('/', function (req, res) {
          res.send(rows);
        });

    });

    app.listen(3002, function () {
      console.log('Example app listening on port 3000!');
    })

这将在我的HTML页面上打印我的sql语句中的所有数据:

This prints all the data from my sql statement on my HTML page:

{"id":"1","name":"Robert"}
{"id":"2","name":"John"}
{"id":"3","name":"Jack"}
{"id":"4","name":"Will"}

我希望得到的结果是:

id              Name
1               Robert
2               John
3               Jack
4               Will
..etc 

甚至可以在Node JS中做到这一点吗?

Is this even possible to do in Node JS?

推荐答案

我建议你看看。它是节点js的模板引擎,可用于创建html页面。它易于使用且非常灵活。

I would recommend you look at Jade. It is a template engine for node js which can be used to create html pages. It is easy to use and very flexible.

找到了一个很好的教程 here 。它向您展示了如何使用Node,Express和Jade创建一个简单的网站,并且在我看来是一个很好的起点。

A good tutorial is found here. It shows you how to create a simple Website with Node, Express and Jade and is a good starting Point in my opinion.

为了解决您的问题,Jade有几个答案在stackoverflow中,如此处

To solve your problem with Jade there are several answers in stackoverflow like here.

这篇关于使用Node.js和Express将JSON数据集显示为表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆