get with params not working
Can someone help me.
i have simple code:
import { Hono } from "hono";
const app = new Hono();
app.get("/", (c) => c.json("list books"));
app.get("/:id", (c) => c.json(
get ${c.req.param("id")}));
export default app;
When try to call api with: curl http://localhost:3000/?id=5 i get response "list books".
Without: app.get("/", (c) => c.json("list books")); i get "404 Not Found"4 Replies
To get the
id
param your path should be http://localhost:3000/5
. By placing the values after the ?
sign you are passing them as query params. So in your case when you are going to /?id=5
, you are basically going to /
route with id=5
as the query param.
That's why you are getting list books
as the response.Thanks it work now. But, how to pass two or more params? or how to make route that is similar to this: 'https://dummyjson.com/products/search?q=phone'
app.get('/posts/:id', (c) => {
const page = c.req.query('page')
const id = c.req.param('id')
c.header('X-Message', 'Hi!')
return c.text(
You want see ${page} of ${id}
)
}) how to call this route when i want to pass id and pageYou can make a route for /posts/:id/:page or you can do an if statement in that route. If there is a queryParam of page then return one thing