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)
})
+40
View File
@@ -0,0 +1,40 @@
import express from 'express'
import {getData4map} from "../actions/data4map.js"
import * as cTable from "../utilities/commands.js"
import * as ERR from "../utilities/errortexts.js"
const router = express.Router();
/* GET home page. */
// normal routes called from javascript client
router.get('/:cmd', async (req, res) => {
const cmd = req.params.cmd
let table
for (let c of cTable.chartcmdTable) {
if (c.cmd === cmd) {
let erg = await c.func(req.query)
if (typeof erg === 'string') {
res.type('text')
res.send(erg)
} else {
res.json(erg)
}
return
}
}
res.json({err: ERR.CMNDUNKOWN})
})
router.post('/:cmd', async (req, res) => {
const cmd = req.params.cmd
if (cmd === 'getdata4maps') {
const erg = await getData4map(req.body)
res.json(erg)
} else {
res.json({err: ERR.CMNDUNKOWN})
}
})
export default router
+2 -30
View File
@@ -1,43 +1,15 @@
import express from 'express'
import {getData4map} from "../actions/data4map.js"
import cmdTable from "../utilities/commands.js"
import * as cTable from "../utilities/commands.js"
import * as ERR from "../utilities/errortexts.js"
const router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.status(200).json({message: ERR.NOTHIMG})
});
// normal routes called from javascript client
router.get('/:cmd', async (req, res) => {
const cmd = req.params.cmd
for (let c of cmdTable) {
if (c.cmd === cmd) {
let erg = await c.func(req.query)
if (typeof erg === 'string') {
res.type('text')
res.send(erg)
} else {
res.json(erg)
}
return
}
}
res.json({err: ERR.CMNDUNKOWN})
})
router.post('/:cmd', async (req, res) => {
const cmd = req.params.cmd
if (cmd === 'getdata4maps') {
const erg = await getData4map(req.body)
res.json(erg)
} else {
res.json({err: ERR.CMNDUNKOWN})
}
})
export default router