-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (62 loc) · 1.92 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
const express = require('express')
const app = express()
const port = 2005
const sqlite3 = require('sqlite3').verbose()
var db = new sqlite3.Database(
'./words.db')
var moment = require('moment')
time = moment().format('YYYY-MM-DD')
let revise_curve = {
1 : 2,
2 : 4,
4 : 7,
7 : 15,
15 : 30,
30 : 60
}
function update_revise_time() {
yesterday = moment().subtract(1, 'days')
yesterday_str = yesterday.format("YYYY-MM-DD")
db.all("SELECT * FROM revise where time =?", [yesterday_str], (err, result) => {
result.forEach((row) => {
word_id = row['word_id']
next_time = row['next_time']
next_time_str = moment().subtract(1, 'day').add(next_time, 'days').format("YYYY-MM-DD")
db.run("UPDATE revise SET time = ?, next_time = ? WHERE word_id =?", [next_time_str, revise_curve[next_time.toString()], word_id])
})
})
}
app.use(express.static('static'))
app.use(express.json()); // 用于解析JSON格式的POST请求
app.use(express.urlencoded({ extended: true })); // 用于解析URL编码的POST请求
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/unit', (req, res) => {
db.all('select distinct words.unit from words', (err, rows) => {
if (err) {
return console.log(err.message)
}
units = rows
res.send(units)
})
})
app.post('/getword', (req, res) => {
db.all('select words.* from words where unit = ?', [req.body["unit"]], (err, rows) => {
if (err) {
return console.log(err.message)
}
words = rows
res.send(words)
})
})
app.post('/getmeaning', (req, res) => {
db.all("select id, pos, meaning_ch from meanings where word_id = ?", [req.body['word_id']], (err, rows) => {
res.send(rows)
})
})
app.post('/update_revise', (req, res) => {
update_revise_time()
res.send("ok")
})
app.listen(port, () => {})