-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmysql_express.js
More file actions
39 lines (34 loc) · 860 Bytes
/
mysql_express.js
File metadata and controls
39 lines (34 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const express = require('express');
const mysql = require('mysql');
// Buat koneksi ke database MySQL
const connection = mysql.createConnection({
host: 'localhost',
user: 'nama_user',
password: 'kata_sandi',
database: 'nama_database'
});
connection.connect((error) => {
if (error) {
console.error(error);
} else {
console.log('Koneksi ke database berhasil');
}
});
// Buat aplikasi express
const app = express();
// Buat route yang akan menampilkan semua data dari tabel
app.get('/', (req, res) => {
const query = 'SELECT * FROM nama_tabel';
connection.query(query, (error, rows) => {
if (error) {
console.error(error);
res.sendStatus(500);
} else {
res.send(rows);
}
});
});
// Mulai server pada port 3000
app.listen(3000, () => {
console.log('Server berjalan pada http://localhost:3000');
});