mirror of
https://github.com/journey-ad/Moe-Counter.git
synced 2024-11-27 17:00:51 +01:00
add getCount method..
1. 添加获取 count 的方法
2. 用更安全的方式获取端口号🙂
This commit is contained in:
parent
d94b2f64ab
commit
fc8af30731
42
index.js
42
index.js
@ -11,6 +11,7 @@ const themify = require('./utils/themify')
|
||||
const PLACES = 7
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(express.static('assets'))
|
||||
app.use(compression())
|
||||
app.set('view engine', 'pug')
|
||||
@ -19,6 +20,21 @@ app.get('/', (req, res) => {
|
||||
res.render('index')
|
||||
});
|
||||
|
||||
const getCountByName = async name=> {
|
||||
if (name === 'demo') return '0123456789'
|
||||
// console.log(name)
|
||||
try {
|
||||
const counter = await db.getNum(name) || { name, num: 0 }
|
||||
const r = counter.num + 1
|
||||
db.setNum(counter.name, r)
|
||||
return r
|
||||
} catch (error) {
|
||||
console.log("get count by name is error: ", error)
|
||||
const errorDefaultCount = 0
|
||||
return errorDefaultCount
|
||||
}
|
||||
}
|
||||
|
||||
// get the image
|
||||
app.get('/get/@:name', async (req, res) => {
|
||||
const name = req.params.name
|
||||
@ -31,23 +47,18 @@ app.get('/get/@:name', async (req, res) => {
|
||||
'cache-control': 'max-age=0, no-cache, no-store, must-revalidate'
|
||||
})
|
||||
|
||||
count = await getCountByName(name)
|
||||
|
||||
if (name === 'demo') {
|
||||
res.set({
|
||||
'cache-control': 'max-age=31536000'
|
||||
})
|
||||
count = '0123456789'
|
||||
length = 10
|
||||
|
||||
} else {
|
||||
const counter = await db.getNum(name) || { name, num: 0 }
|
||||
count = counter.num + 1
|
||||
|
||||
db.setNum(counter.name, count)
|
||||
console.log(counter, `theme: ${theme}`)
|
||||
}
|
||||
|
||||
// Send the generated SVG as the result
|
||||
res.send(themify.getCountImage({ count, theme, length }))
|
||||
const renderSvg = themify.getCountImage({ count, theme, length })
|
||||
res.send(renderSvg)
|
||||
})
|
||||
|
||||
app.get('/heart-beat', (req, res) => {
|
||||
@ -59,6 +70,17 @@ app.get('/heart-beat', (req, res) => {
|
||||
console.log('heart-beat')
|
||||
});
|
||||
|
||||
const listener = app.listen(config.app.port, () => {
|
||||
let port = 3000
|
||||
|
||||
try {
|
||||
let configPort = config.app.port
|
||||
if (configPort) {
|
||||
port = configPort
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(error)
|
||||
}
|
||||
|
||||
const listener = app.listen(port, () => {
|
||||
console.log('Your app is listening on port ' + listener.address().port)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user