Today we will create a route with path parameters. Fortunately, Cavia's parameter parsing mechanism is built-in and using it is very simple.
Step 1: New endpoint
Create a new endpoint with path params - to indicate the position of a parameter in the path, add a colon in front of the parameter name.
import { Route } from '@caviajs/http-router';
export const GuineaPigDetailsRoute: Route = {
handler: async (request, response): Promise<void> => {
const id = request.params.id; // 👈
console.log(id); // 👈 do sth...
},
method: 'GET',
path: '/guinea-pigs/details/:id', // 👈
};
Remember to add the newly created route to the HttpRouter
instance and start the server.
Step 2: Make request
Make a request to newly created endpoint.
curl -XGET 'http://localhost:8080/guinea-pigs/details/1245'
Yes, it's that simple.
See you!
Top comments (0)