Some errors fixed, works for api.js

This commit is contained in:
rxf
2023-03-24 15:30:41 +01:00
parent c4c90b4455
commit 26e4b75a6f
7 changed files with 134 additions and 76 deletions
+30
View File
@@ -0,0 +1,30 @@
import express from 'express'
import {getData4map} from "../actions/data4map.js"
import * as cTable from "../utilities/commands.js"
import * as ERR from "../utilities/errortexts.js"
export const apiRouter = express.Router();
export const dispatchCommand = async (cmd, table, query, res) => {
for (let c of table) {
if (c.cmd === cmd) {
let erg = await c.func(query)
if (typeof erg === 'string') {
res.type('text')
res.send(erg)
} else {
res.json(erg)
}
return
}
}
res.json({err: ERR.CMNDUNKOWN})
}
// normal routes called from javascript client
apiRouter.get('/:cmd', async (req, res) => {
await dispatchCommand(req.params.cmd, cTable.cmdTable, req.query, res)
})