From 4daac0781f3910a611d72318bf48cca2a1e2b0e6 Mon Sep 17 00:00:00 2001 From: d1y Date: Sat, 22 Aug 2020 00:24:02 +0800 Subject: [PATCH] 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({