Js generating function parameters

there are a batch of statements to be written as follows (koa routing configuration), and the form is fixed as follows

router.get("/pattern", api.getPattern)
router.post("/pattern", api.setPattern)
router.get("/road", api.getRoad)
router.post("/road", api.setRoad)
......

if you want to generate this large number of statements quickly, how to deal with it

Mar.01,2021

What about the

variable?

A for loop is done


function firstUpperCase(str) {
    return str.toLowerCase().replace(/( |^)[a-z]/g, (l) => l.toUpperCase());
}

function add(name) {
    ['post', 'get'].forEach(m => {
        router[m](`/${name}`, api[m + firstUpperCase(name)])
    })
}

add('pattern')
Menu