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.
Code that I use:
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!');
})
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"}
what I would like to have as result is :
id Name
1 Robert
2 John
3 Jack
4 Will
..etc
Is this even possible to do in Node JS?
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.
Code that I use:
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!');
})
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"}
what I would like to have as result is :
id Name
1 Robert
2 John
3 Jack
4 Will
..etc
Is this even possible to do in Node JS?
Share Improve this question edited Mar 3, 2016 at 7:50 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 15, 2016 at 11:24 Rahul SharmaRahul Sharma 1901 gold badge3 silver badges18 bronze badges 5- I am trying to create an table. Sorry for the wrong suggestions. – Rahul Sharma Commented Jan 15, 2016 at 11:37
- So you're trying to display the resultset in a table in HTML? (Creating a table is, in database terms, a different thing) – leroydev Commented Jan 15, 2016 at 11:37
- I would like to create an table into my HTML page not in my database – Rahul Sharma Commented Jan 15, 2016 at 11:38
- There's multiple approaches to this, you could render it as a table serverside or you could fetch the JSON from the clientside, loop through it and make the table there, which one do you prefer? – leroydev Commented Jan 15, 2016 at 11:40
- I think the best way to do it for me is using JSON – Rahul Sharma Commented Jan 15, 2016 at 12:06
2 Answers
Reset to default 6Similar to Jade you could use Embedded JS
<table>
<tr>
<th>id</th><th>Name</th>
</tr>
<% for (var i = 0; i < data.length; i++) { %>
<tr>
<td><%= data[i].id %></td>
<td><%= data[i].name %></td>
</tr>
<% } %>
</table>
This would iterate through an array of objects (which I have saved as data
and populate a table based on that.
I would remend 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.
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.
To solve your problem with Jade there are several answers in stackoverflow like here.