[Fixed] ExpressJS router with parameter empty

Issue

I’m working on my first Rest API with NodeJS using ExpressJS. It’s very simple and intuitive.
One of my routing rule is:

api.get("/api/plugin/:action", (req,res) => { ... }

If I send:

http://localhost:3000/api/plugin

I got the error:

Cannot get /api/plugin/

Is ther a way to intercept this single case, excluding the routing without :action? I need to check req.param, and verify the action param, even if it is empty.

Solution

you can use ? in route to make your parameter optional

api.get("/api/plugin/:action?", (req,res) => { ... }

Leave a Reply

(*) Required, Your email will not be published