Last active
March 16, 2022 12:17
-
-
Save pama/a10aaa93cb971cc3f1e0cf330102cdda to your computer and use it in GitHub Desktop.
Show data in a table using Express.JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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