-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.js
77 lines (65 loc) Β· 1.83 KB
/
server.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express');
const mysql = require('mysql');
const app = express();
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
app.set('port', process.env.PORT || 3001);
// Express only serves static assets in production
console.log('NODE_ENV: ', process.env.NODE_ENV);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
// Return the main index.html, so react-router render the route in the client
app.get('/', (req, res) => {
res.sendFile(path.resolve('client/build', 'index.html'));
});
}
const host = 'localhost';
const user = 'root';
const pswd = 'password';
const dbname = 'books';
// config db ====================================
const pool = mysql.createPool({
host: host,
user: user,
password: pswd,
port: '3306',
database: dbname
});
const COLUMNS = ['last_name', 'first_name'];
app.get('/api/books', (req, res) => {
const firstName = req.query.firstName;
if (!firstName) {
res.json({
error: 'Missing required parameters'
});
return;
}
let queryString = ``;
if (firstName == '*') {
queryString = `SELECT * from authors`;
} else {
queryString = `SELECT * from authors WHERE first_name REGEXP '^${firstName}'`;
}
pool.query(queryString, function(err, rows, fields) {
if (err) throw err;
if (rows.length > 0) {
res.json(
rows.map(entry => {
const e = {};
COLUMNS.forEach(c => {
e[c] = entry[c];
});
return e;
})
);
} else {
res.json([]);
}
});
});
app.listen(app.get('port'), () => {
console.log(`Find the server at: http://localhost:${app.get('port')}/`); // eslint-disable-line no-console
});