-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (94 loc) · 3.12 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
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
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mysql = require('mysql');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//mysql connections
var pool = mysql.createPool({
connectionLimit : 100, //important
host : 'localhost',
user : 'root',
password : '',
database : 'qp',
debug : false
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//app.use(express.static(path.join(__dirname, 'public')));
//index page
app.get('/',function(req,res){
console.log('index page directed');
res.render('index');
});
//dumping data
function handle_database(req,res,data1) {
console.log('here');
pool.getConnection(function(err,connection){
if (err) {
connection.release();
res.json({"code" : 100, "status" : "Error in connection database"});
return;
}
console.log('connected as id ' + connection.threadId);
connection.query('select * from question',function(err,rows){
connection.release();
if(!err) {
console.log('done');
var i;
var duration = 0;
var for_done = 0;
data1 *= 60;
//console.log("data1 is :"+ data1);
//console.log("rows.length : "+rows.length);
/*rows.forEach(function(r){
duration+=r.qduration;
});*/
for(i=0;i<rows.length;i++){
duration += rows[i].qduration;
//console.log("duration is "+duration)
if(duration <= data1){
//console.log("duration = " + duration);
}
else{
for_done = 1;
break;
}
if(i==(rows.length-1)){
for_done = 1;
}
}
if(for_done==1){
//i=i-1;
//console.log("i is : ", i);
//console.log("duration is : ", duration);
var data = [];
for(j=0;j<i;j++){
var x= {};
x = rows[j];
//console.log(x);
data.push(x);
console.log(data.length);
}
console.log(data);
console.log(data[1].qtext);
res.render('question_paper',{data : data});
}
}
});
connection.on('error', function(err) {
//res.json({"code" : 100, "status" : "Error in connection database"});
return;
});
});
}
//getting data from index page and rendering question paper page
app.post('/question_paper',function(req,res){
var data = req.body.duration;
console.log("data of duration : " + data);
handle_database(req,res,data);
});
app.listen(3010);
module.exports = app;