-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
157 lines (142 loc) · 4.5 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var mysql = require('mysql');
var express = require('express');
var hbs = require('hbs');
var bodyParser = require('body-parser');
var jwt = require('jsonwebtoken');
var bcryptjs = require('bcryptjs');
const spawn = require("child_process").spawn;
const { exec } = require('child_process');
// exec('python3 Text_Gen.py', (err, stdout, stderr) => {
// if (err) {
// console.error(`exec error: ${err}`);
// return;
// }
//
// console.log(`Number of files ${stdout}`);
// });
// var myPythonScriptPath = 'Text_Gen.py';
// Use python shell
// var PythonShell = require('python-shell');
// var pyshell = PythonShell.run(myPythonScriptPath);
// pyshell.on('message', function (message) {
// // received a message sent from the Python script (a simple "print" statement)
// console.log(message);
// });
// end the input stream and allow the process to exit
// pyshell.end(function (err) {
// if (err){
// throw err;
// };
//
// console.log('finished');
// });
function connect() {
var connection = mysql.createConnection({
host: 'localhost',
user: 'hallucinators',
password: 'hallucinators',
database: 'hallucinators'
})
connection.connect
return connection
}
var app = express();
app.set('view engine', 'hbs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(__dirname + '/client'));
app.post('/check', (req, res) => {
LNO = req.body.LNO;
console.log(LNO);
// console.log(LNO);
try {
var connection = connect()
console.log("done!")
connection.query('select * from vehicles where license like \'' + LNO + '\';', (err, rows, fields) => {
connection.end
if (!err) {
if (rows.length && rows[0].due_amount != null) {
console.log("found" + rows[0].due_amount)
res.render('index.hbs', {
amount: rows[0].due_amount
})
} else res.render('index.hbs', {
amount: 'License Number Does Not Exist in Record!!'
})
} else throw Error(e)
})
} catch (e) {
console.log(e)
res.send('Internal Server Error!')
}
});
app.get('/photo', async (req, res) => {
// console.log(LNO);
try {
exec('python3 Text_Gen.py', (err, stdout, stderr) => {
if (err) {
console.error(`exec error: ${err}`);
// return;
}
console.log(`${stdout}`);
res.render('toll_index.hbs',{
number: stdout,
fee:49
});
});
// new Promise((resolve, reject) => {
// const pythonProcess = spawn('python3', ["Text_Gen.py"], {shell: true, stdio: 'inherit'});
// pythonProcess.stdout.on('data', (data) => {
// console.log(`Number of files ${data}`);
// res.send(`${data}`)
// });
// resolve(pythonProcess)
// }).then((result) => {
// console.log(result)
// res.send(result)
// })
// pythonshell.run('Text_Gen.py', options, function (err, results) {
// if (err) throw err;
// // results is an array consisting of messages collected during execution
// console.log('results: %j', results);
// });
} catch (e) {
console.log(e)
res.send('Internal Server Error!')
}
});
// app.get('/tolllogin', (req, res) => {
// res.render('toll_login.hbs', {});
// })
// app.get('/toll', (req, res) => {
// res.render('index.hbs', {
// number: "GJ-05-1996"
// });
// });
app.post('/store',(req,res)=> {
number = req.body.number,
fee = req.body.fee
try {
var connection = connect()
console.log("connected!")
connection.query('update vehicles set due_amount = due_amount+'+fee+' where license like \'' + number + '\';', (err, rows, fields) => {
connection.end
if (!err) {
if (rows.changedRows) {
res.render('result.hbs', {status: "Toll added!\\nYou will be Redirected to HOME PAGE."})
} else {
res.render('result.hbs', {status: "Invalid License Number"})
}
console.log(rows)
} else throw Error(e)
})
} catch (e) {
console.log(e)
res.send('Internal Server Error!')
}
})
app.listen(3000, (err, res) => {
if (!err)
console.log("connected to port 3000");
})