-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevServer.js
More file actions
100 lines (89 loc) · 2.73 KB
/
devServer.js
File metadata and controls
100 lines (89 loc) · 2.73 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const config = require('./webpack.config.dev');
const argv = require('minimist')(process.argv.slice(2));
const app = express();
const compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
const mysql = require('mysql')
const pool = mysql.createPool({
host: argv.h,
user: argv.u,
password: argv.p,
database: argv.d,
charset: 'tis620_thai_ci'
})
pool.getConnection((err, connection) => {
if (err) {
console.error('Error Connecting: ' + err.stack)
process.exit(1)
}
connection.query('SELECT * FROM kskdepartment', (err, result) => {
if (err) {
console.error('Error Connecting: ' + err.stack)
process.exit(1)
}
})
connection.release()
})
app.get('/api/:depId', (req, res) => {
res.setHeader('Content-Type', 'application/json')
if (!parseInt(req.params.depId)) {
res.statusCode = 400;
res.json({error: `Cannot parse ${req.params.depId}`})
res.end()
return;
}
pool.getConnection((err, connection) => {
if (err) {
console.error('error connecting: ' + err.stack);
res.statusCode = 500;
res.json({error: `Cannot ${req.method} ${req.url}`, stack: err})
res.end()
return
}
connection.query('SET NAMES UTF8')
connection.query(`SELECT kskdepartment.department as name, opduser.name as doctor_name, opduser.entryposition as doctor_position FROM kskdepartment LEFT JOIN opduser ON kskdepartment.doctor_code = opduser.doctorcode WHERE kskdepartment.depcode = ${parseInt(req.params.depId)}`, (err, department) => {
if (err) {
res.statusCode = 500;
res.json({error: `Cannot ${req.method} ${req.url}`, stack: err})
res.end()
return;
}
if (department.length) {
connection.query(`SELECT ovst.oqueue as queue_number, CONCAT(patient.pname, patient.fname, ' ', patient.lname) as patient_name FROM ovst INNER JOIN patient ON ovst.hn = patient.hn WHERE ovst.cur_dep = ${parseInt(req.params.depId)} ORDER BY cur_dep_time DESC LIMIT 5`, (err, queue) => {
if (err) {
res.statusCode = 500;
res.json({error: `Cannot ${req.method} ${req.url}`, stack: err})
res.end()
return;
}
res.send({
department: department[0],
queue
})
})
connection.release()
return;
}
res.statusCode = 500;
res.json({error: `Department Id: ${req.params.depId} not found`})
connection.release()
})
})
})
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(3000, 'localhost', (err) => {
if (err) {
console.log(err);
return;
}
console.log('Listening at http://localhost:3000');
});