Pathname prefix in serve()

Hi, I was wondering if it's possible to only handle requests coming from specific prefix within pathname like: localhost:8000/api I'm using std serve.
6 Replies
NDH
NDH2y ago
let { pathname } = new URL(request.url);

if (pathname.includes('/api')) {
//do something with api
}
let { pathname } = new URL(request.url);

if (pathname.includes('/api')) {
//do something with api
}
taiwannumberone
thanks, but in handler function for serve, is it possible to not return any response?
NDH
NDH2y ago
I don't think so, but you could just send an empty or custom response.
ioB
ioB2y ago
why would you not send a response if no response was sent back the client would just be left hanging but with an open tcp connection
taiwannumberone
I'm creating simple api using std that would run alongside with my page made in deno fresh. I thoguht that calling 404 response from api would interfere with fresh pages? Not sure about that so i may be just wrong. thanks for the answers tho <:cookie_deno:1002977285734932480>
NDH
NDH2y ago
Fresh handles apis very effectively. Just use Fresh. Let me know if you need an example. This api named post - /routes/api/post.ts -, justs relays messages to all BroadcastChannel subscribers. (all isolates) https://github.com/nhrones/FreshDiceRTC/blob/master/routes/api/post.ts Make sure to run deno-task-start after you add a route.