-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (49 loc) · 1.19 KB
/
index.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
53
54
55
56
const express = require('express');
const cors = require('cors');
const mysql = require('mysql');
// start app
const app = express();
app.use(cors());
// start db connection
// TODO: move to .env
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'covidentify'
});
db.connect((err) => {
if (err) {
console.log('db connection failed!');
throw err;
} else {
console.log('MySQL Connected...');
}
})
// routes
app.get('/', (_, res) => {
res.send('hello world!')
});
// Create db
app.get('/createdb', (req, res) => {
let query = 'CREATE DATABASE covidentify';
db.query(query, (err, qres) => {
if(err) throw err;
console.log('query result:', qres);
res.send('Database created...')
})
})
// Create db table
app.get('/createtable', (req, res) => {
let query = 'CREATE TABLE data(id int AUTO_INCREMENT, email VARCHAR(255), PRIMARY KEY (id))';
db.query(query, (err, qres) => {
if(err) throw err;
console.log('query result:', qres);
res.send('Data table created...')
})
})
// listen
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`COVIDentify API listening on port ${port}!`);
})