-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
52 lines (42 loc) · 1.11 KB
/
app.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
const express = require('express')
const ip = require('ip')
const mysql = require('./database')();
const app = express()
const port = 3000
const connection = mysql.init();
mysql.db_open(connection);
app.get('/', (req, res) => {
connection.query('SELECT * FROM USER',
function (error, results, fields) {
if (error) {
res.send(error);
}
// return current server ip address
res.send(`Hello World! (ip: ${ip.address()})`);
});
})
app.get('/name', (req, res) => {
connection.query('SELECT * FROM USER',
function (error, results, fields) {
if (error) {
res.send(error);
}
// send query result as json
res.json(results);
});
})
// get name and insert into mysql
app.get('/name/:name', (req, res) => {
const name = req.params.name;
connection.query(`INSERT INTO USER (name) VALUES ('${name}')`,
function (error, results, fields) {
if (error) {
res.send(error);
}
// send query result as json
res.send('success');
});
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})