-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
60 lines (46 loc) · 1.75 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
const express = require('express');
require('dotenv').config();
const path = require('path');
const fs = require('fs');
const app = express();
app.use(express.json()); // Parse JSON data
app.use(express.urlencoded({ extended: false }))
const appPORT = process.env.APP_PORT;
//Checking if progress file exists!
const sqlite3 = require('sqlite3').verbose();
const dbOps = require('./db');
if (fs.existsSync('progress/database.db'));
else dbOps.initProgress();
// Set EJS as the templating engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Route to render the index.ejs
const db = new sqlite3.Database('progress/database.db');
const appFOR = process.env.APP_FOR;
app.get('/', async (req, res) => {
const countAll = await dbOps.squery(db, 'SELECT count(*) as count FROM words;');
const status = await dbOps.query(db, 'SELECT DISTINCT status from words;');
let statusArr = [];
// status.forEach(async i => {
// if(i.status){
// const x = (await dbOps.squery(db, `SELECT count(*) as count FROM words where status = ${i.status};`)).count;
// statusArr.push(x);
// }
// });
res.render('index', {wordsCount:countAll.count, appFOR:appFOR});
});
app.post("/processQuestion", async (req, res) => {
await dbOps.dbCommand(db, `UPDATE words SET status=${req.body.score}, updated_at=${Date.now()} where id = ${req.body.id}`).then(()=>{
res.send("success!");
});
});
app.post("/getRandomQuestion", (req, res)=>{
db.all('SELECT * FROM words where status is null limit 1', (err, row) => {
res.json(row[0]);
});
});
app.listen(appPORT, () => {
console.log(`Server is running on http://localhost:${appPORT}`);
});