add return json data REST api

获取@name数据(json)
This commit is contained in:
d1y 2020-08-22 00:24:02 +08:00
parent fc8af30731
commit 4daac0781f

View File

@ -21,13 +21,13 @@ app.get('/', (req, res) => {
}); });
const getCountByName = async name=> { const getCountByName = async name=> {
if (name === 'demo') return '0123456789'
// console.log(name) // console.log(name)
if (name === 'demo') return { num: '0123456789', name }
try { try {
const counter = await db.getNum(name) || { name, num: 0 } const counter = await db.getNum(name) || { name, num: 0 }
const r = counter.num + 1 const r = counter.num + 1
db.setNum(counter.name, r) db.setNum(counter.name, r)
return r return counter
} catch (error) { } catch (error) {
console.log("get count by name is error: ", error) console.log("get count by name is error: ", error)
const errorDefaultCount = 0 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 // get the image
app.get('/get/@:name', async (req, res) => { app.get('/get/@:name', async (req, res) => {
const name = req.params.name 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' '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') { if (name === 'demo') {
res.set({ res.set({