DEV Community

loading...

Answer: How to call dynamic controller function from a same endpoint of a route in Node JS?

anujpancholi profile image Anuj Pancholi ・1 min read

I made and tested a quick dummy express app with a possible solution - using res.redirect()

const express = require('express')
const app = express()

app.use((req,res,next) => {
    if(req.path === "/my-home" && req.headers['item']){
        res.redirect(`/my-home/use-item-handler/${req.headers['item']}`)
    }

    next()
})


const handler1 = (req,res,next) => {
    res.json({
        item: "h1" 
    })
}

const handler2 =

Discussion

pic
Editor guide