-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (62 loc) · 1.79 KB
/
Copy pathserver.js
File metadata and controls
76 lines (62 loc) · 1.79 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
const {Client} = require('pg')
const express = require('express')
const app = express()
app.use(express.json())
require('dotenv').config()
// console.log(process.env)
const port = process.env.PORT || 5050;
var conString = process.env.CONN_STRING
const client = new Client(conString)
// app.get("/", (req,res) => res.sendFile(`${__dirname}/index.html`))
app.get("/attendance/:id", async(req,res) => {
var id = parseInt(req.params.id)
const rows = await readAttendance(id);
res.setHeader("content-type","application/json")
res.send(JSON.stringify(rows))
})
app.get("/attendance/:id/:month/:year" , async (req,res) => {
var id = parseInt(req.params.id)
var month = req.params.month
var year = parseInt(req.params.year)
console.log(month,year)
const result = await readAttendanceByDate(id,month,year)
res.setHeader("content-type", "application/json")
res.send(JSON.stringify(result))
})
app.listen(port, () => console.log(`Web Server is Listening...on port ${port}`,))
start()
async function start() {
await connect();
/*
const result = await readAttendance();
console.log(result)
*/
// await client.end()
}
async function connect(){
try{
await client.connect();
}
catch(e){
console.error(`Failed to connect ${e}`)
}
}
async function readAttendanceByDate(rollNO,month,year){
try{
const result = await client.query(`SELECT * FROM attendance WHERE date LIKE '${month}____${year}' AND roll = ${rollNO}`)
return result.rows
}
catch(e){
return []
}
}
async function readAttendance(rollNO){
try{
const result = await client.query(`SELECT * FROM attendance WHERE roll = ${rollNO}`)
// client.end()
return result.rows
}
catch(e){
return []
}
}