You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The simplest, and recommended form works as follows:
import{Application}from'@curveball/kernel';importrouterfrom'@curveball/router';constapp=Application();app.use(router('/foo/:id',ctx=>{// the 'id' is available via ctx.params.id}));
It's also possible to do per-method routing, using the following syntax.
import{Application}from'@curveball/kernel';importrouterfrom'@curveball/router';constapp=Application();app.use(router('/foo/:id').get(ctx=>{/* GET requests */}).post(ctx=>{/* POST requests */}));
You can either specify 1, or multiple middlewares. The following example runs
2 fictional middlewares on a route.
The matched route is added into the Curveball context for other middleware to access
if they need (such as for access request logging). It will be accessible after the
router has executed.
import{Application,Context}from'@curveball/kernel';importrouterfrom'@curveball/router';constapp=Application();app.use(async(ctx: Context,next)=>{awaitnext();// Will be '/foo/:id'constmatchedRoute=ctx.router?.matchedRoute;});app.use(router('/foo/:id',ctx=>{ctx.response.body='/foo/'+ctx.state.params.id;}););