From d94b2f64ab01fe16fbd45983d828abb3e687fe58 Mon Sep 17 00:00:00 2001 From: d1y Date: Sat, 22 Aug 2020 00:11:20 +0800 Subject: [PATCH 1/5] add default mongodb url(local server) --- db/mongodb.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/db/mongodb.js b/db/mongodb.js index fa7e96d..40f7695 100644 --- a/db/mongodb.js +++ b/db/mongodb.js @@ -3,7 +3,12 @@ const mongoose = require('mongoose') const schema = require('./schema') -mongoose.connect(process.env.DB_URL, { +// the default mongodb url (local server) +let mongodbURL = `mongodb://127.0.0.1:27017` +const envMongoURL = process.env.DB_URL +if (envMongoURL) mongodbURL = envMongoURL + +mongoose.connect(mongodbURL, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false From fc8af30731e1e672701ea243efb488a2b5286fbb Mon Sep 17 00:00:00 2001 From: d1y Date: Sat, 22 Aug 2020 00:14:01 +0800 Subject: [PATCH 2/5] add getCount method.. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 添加获取 count 的方法 2. 用更安全的方式获取端口号🙂 --- index.js | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 81d7364..34d4e2c 100644 --- a/index.js +++ b/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) }) From 4daac0781f3910a611d72318bf48cca2a1e2b0e6 Mon Sep 17 00:00:00 2001 From: d1y Date: Sat, 22 Aug 2020 00:24:02 +0800 Subject: [PATCH 3/5] add return json data REST api MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 获取@name数据(json) --- index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 34d4e2c..f5614c3 100644 --- a/index.js +++ b/index.js @@ -21,13 +21,13 @@ app.get('/', (req, res) => { }); const getCountByName = async name=> { - if (name === 'demo') return '0123456789' // console.log(name) + if (name === 'demo') return { num: '0123456789', name } try { const counter = await db.getNum(name) || { name, num: 0 } const r = counter.num + 1 db.setNum(counter.name, r) - return r + return counter } catch (error) { console.log("get count by name is error: ", error) const errorDefaultCount = 0 @@ -35,6 +35,21 @@ const getCountByName = async name=> { } } +// the rest api get data +// link: https://www.liaoxuefeng.com/wiki/1022910821149312/1105009634703392 +app.get('/rest/@:name', async (req, res) => { + const name = req.params.name + try { + const data = await getCountByName(name) + res.send(data) + } catch (error) { + res.send({ + num: 0, + name + }) + } +}) + // get the image app.get('/get/@:name', async (req, res) => { const name = req.params.name @@ -47,7 +62,8 @@ app.get('/get/@:name', async (req, res) => { 'cache-control': 'max-age=0, no-cache, no-store, must-revalidate' }) - count = await getCountByName(name) + const data = await getCountByName(name) + count = data.num if (name === 'demo') { res.set({ From 56c4a37f6dced77f785740b55d2b104071f6907f Mon Sep 17 00:00:00 2001 From: Jad Date: Sat, 22 Aug 2020 20:36:34 +0800 Subject: [PATCH 4/5] Update mongodb.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 简化代码 --- db/mongodb.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/db/mongodb.js b/db/mongodb.js index 40f7695..57f1b72 100644 --- a/db/mongodb.js +++ b/db/mongodb.js @@ -4,9 +4,7 @@ const mongoose = require('mongoose') const schema = require('./schema') // the default mongodb url (local server) -let mongodbURL = `mongodb://127.0.0.1:27017` -const envMongoURL = process.env.DB_URL -if (envMongoURL) mongodbURL = envMongoURL +const mongodbURL = process.env.DB_URL || 'mongodb://127.0.0.1:27017' mongoose.connect(mongodbURL, { useNewUrlParser: true, @@ -38,4 +36,4 @@ module.exports = { getNum, getAll, setNum -} \ No newline at end of file +} From 398f0ff6ba73dbc54b18f6c68a979bba80b527f8 Mon Sep 17 00:00:00 2001 From: Jad Date: Sat, 22 Aug 2020 21:20:20 +0800 Subject: [PATCH 5/5] Update index.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改了一些写法和逻辑 没必要处理所有的异常,比如配置文件出错这类启动时就能发现的 --- index.js | 81 ++++++++++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index f5614c3..88e6828 100644 --- a/index.js +++ b/index.js @@ -20,41 +20,11 @@ app.get('/', (req, res) => { res.render('index') }); -const getCountByName = async name=> { - // console.log(name) - if (name === 'demo') return { num: '0123456789', name } - try { - const counter = await db.getNum(name) || { name, num: 0 } - const r = counter.num + 1 - db.setNum(counter.name, r) - return counter - } catch (error) { - console.log("get count by name is error: ", error) - const errorDefaultCount = 0 - return errorDefaultCount - } -} - -// the rest api get data -// link: https://www.liaoxuefeng.com/wiki/1022910821149312/1105009634703392 -app.get('/rest/@:name', async (req, res) => { - const name = req.params.name - try { - const data = await getCountByName(name) - res.send(data) - } catch (error) { - res.send({ - num: 0, - name - }) - } -}) - // get the image app.get('/get/@:name', async (req, res) => { - const name = req.params.name - const theme = req.query.theme || 'moebooru' - let length = PLACES, count = 0 + const { name } = req.params + const { theme = 'moebooru' } = req.query + let length = PLACES // This helps with GitHub's image cache res.set({ @@ -63,7 +33,6 @@ app.get('/get/@:name', async (req, res) => { }) const data = await getCountByName(name) - count = data.num if (name === 'demo') { res.set({ @@ -73,8 +42,19 @@ app.get('/get/@:name', async (req, res) => { } // Send the generated SVG as the result - const renderSvg = themify.getCountImage({ count, theme, length }) + const renderSvg = themify.getCountImage({ count: data.num, theme, length }) res.send(renderSvg) + + console.log(data, `theme: ${theme}`) +}) + +// JSON record +app.get('/record/@:name', async (req, res) => { + const { name } = req.params + + const data = await getCountByName(name) + + res.json(data) }) app.get('/heart-beat', (req, res) => { @@ -86,17 +66,24 @@ app.get('/heart-beat', (req, res) => { console.log('heart-beat') }); -let port = 3000 - -try { - let configPort = config.app.port - if (configPort) { - port = configPort - } -} catch (error) { - throw new Error(error) -} - -const listener = app.listen(port, () => { +const listener = app.listen(config.app.port || 3000, () => { console.log('Your app is listening on port ' + listener.address().port) }) + +async function getCountByName(name) { + const defaultCount = { name, num: 0 } + + if (name === 'demo') return { name, num: '0123456789' } + + try { + const counter = await db.getNum(name) || defaultCount + const num = counter.num + 1 + db.setNum(counter.name, num) + return counter + + } catch (error) { + console.log("get count by name is error: ", error) + return defaultCount + + } +}