Skip to content

Instantly share code, notes, and snippets.

@pama
Last active March 16, 2022 12:17
Show Gist options
  • Save pama/a10aaa93cb971cc3f1e0cf330102cdda to your computer and use it in GitHub Desktop.
Save pama/a10aaa93cb971cc3f1e0cf330102cdda to your computer and use it in GitHub Desktop.
Show data in a table using Express.JS
const express = require('express')
const app = express()
app.set('view engine', 'pug');
app.set('views','./views');
app.get('/', function (req, res) {
var users = [];
var mysql = require('mysql')
var connection = mysql.createConnection({
host : '********',
user : '********',
password : '********',
database : '********'
});
connection.connect()
connection.query('select * from users', function (err, rows, fields) {
for (var i = 0; i < rows.length; i++) {
// Create an object to save current row's data
var person = {
'id':rows[i].id,
'name':rows[i].name
}
// Add object into array
users.push(person);
}
res.render('index', {"users": users});
})
connection.end()
})
app.listen(3000, () => console.log('Example app listening on port 3000!'))
doctype html
html(lang="en")
head
title ExpressJS Example with MySQL
body
h1 Amazing example
table
for user in users
tr
td= user.id
td= user.name
create table users(
id serial primary key AUTO_INCREMENT,
name varchar(50)
);
INSERT INTO users (name) VALUES ('John Doe');
INSERT INTO users (name) VALUES ('Jane Doe');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment