-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
156 lines (123 loc) · 4.83 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
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
//--------------------------------------------------------------------
// File information.
// Name: app.js
// Purpose: node.js app main file
// License: TBD
// Revision: 220402
//--------------------------------------------------------------------
// Disclaimer.
// This software is provided "AS IS" AND THE AUTHOR DISCLAIMS ALL WAR-
// RANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRAN-
// TIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
// BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAM-
// AGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TOR-
// TIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PER-
// FORMANCE OF THIS SOFTWARE.
//--------------------------------------------------------------------
// Overview.
//### Explain CRUD API here,
//--------------------------------------------------------------------
// Program parameters.
var dbdir = './database' ;
var dbfile = 'helpline.db' ;
var dbpath = dbdir + '/' + dbfile ;
var port = process.env.PORT || 3033 ;
var hostname = '0.0.0.0' ;
//--------------------------------------------------------------------
var sqlite3 = require ('sqlite3').verbose() ;
var express = require ('express') ;
var app = express() ;
//### How is error here handled?
//### What creates the subdir?
//###
var db = new sqlite3.Database (dbpath) ;
app.set("view engine","ejs") ;
app.use(express.static(__dirname + '/public')) ;
//--------------------------------------------------------------------
// Utility routine.
function HandleResult (err, str_op_type) {
var foo;
if (err) {
console.error(err.message);
return "Error during" + str_op_type;
}
foo = str_op_type + " successful";
console.log (foo);
return foo;
}
//--------------------------------------------------------------------
// DB Handle
// db.run ('CREATE TABLE IF NOT EXISTS emergency(id TEXT, name TEXT)');
//--------------------------------------------------------------------
// HomePage
app.get("/",function(req,res){
res.render("home.ejs");
});
//--------------------------------------------------------------------
// Operation: Read. ### different from other Read how?
app.get ('/view/:country', function (req, res) {
db.serialize (() => {
db.each (`SELECT Country COUNTRY, Emergency EMERGENCY,
Police POLICE, Ambulance AMBULANCE, Fire FIRE,
"Call Codes" CALLCODES FROM helpcode WHERE UPPER(Country) = ?
UNION ALL
SELECT NULL, NULL, NULL, NULL, NULL, NULL
LIMIT 1;`,
[req.params.country.toUpperCase()], function (err, row) {
if (err) {
res.send ("[GET]: failed on /view/"+req.params.country);
return console.error (err.message);
}
res.send(row);
/*
res.send(`COUNTRY: ${row.COUNTRY}, ` +
`EMERGENCY: ${row.EMERGENCY}, POLICE: ${row.POLICE}, ` +
`AMBULANCE: ${row.AMBULANCE}, FIRE: ${row.FIRE}, ` +
`CALL CODES: ${row.CALLCODES} \n`);
*/
console.log ("[GET]: successful on /view/"+req.params.country);
});
});
});
//--------------------------------------------------------------------
// ### What? ALL DB
app.get ('/allhelp', function (req, res) {
var records = [];
db.serialize(() => {
db.all (`SELECT Country COUNTRY, Emergency EMERGENCY,
Police POLICE, Ambulance AMBULANCE, Fire FIRE,
"Call Codes" CALLCODES FROM helpcode ORDER BY Country`,
[], (err , rows) => {
if (err) {
console.log ("[GET]: failed on /allhelp")
console.log (err.message);
//### should this return or throw an error or what?
}
res.send(rows);
console.log ("[GET]: successful on /allhelp");
/*
// console.log (rows);
rows.forEach ((row) => {
res.write(`COUNTRY: ${row.COUNTRY}, ` +
`EMERGENCY: ${row.EMERGENCY}, POLICE: ${row.POLICE}, ` +
`AMBULANCE: ${row.AMBULANCE}, FIRE: ${row.FIRE}, ` +
`CALL CODES: ${row.CALLCODES} \n`);
})
res.end();
*/
});
});
});
//--------------------------------------------------------------------
// Fallback Request.
app.get("*", (req,res) => {
res.render("404.ejs");
});
//--------------------------------------------------------------------
// Main program (Listener).
app.listen (port, hostname, function() {
console.log ("Server started on port: "+ port);
});
//--------------------------------------------------------------------
// End of file.