WaspW
Wasp11mo ago
Alex

Can't get apiNamespace working in OpenSaas

Trying to set up API namespace, but curl -v http://localhost:3000/api/ returns landing page HTML instead of hitting my API.

main.wasp
apiNamespace api {
  middlewareConfigFn: import { apiKeyAuthMiddlewareConfigFn } from "@src/server/middlewares/apiKeyAuth",
  path: "/api"
}

middleware (apiKeyAuth.ts)
export const apiKeyAuthMiddlewareConfigFn = (middlewareConfig: Map<string, any>) => {
  middlewareConfig.set('custom.middleware', (_req, _res, next) => {
    console.log('Middleware triggered')
    next()
  })
  return middlewareConfig
}

curl response

* Host localhost:3000 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:3000...
* connect to ::1 port 3000 from ::1 port 50864 failed: Connection refused
*   Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000
> GET /api/ HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.6.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Vary: Origin
< Content-Type: text/html
< Cache-Control: no-cache
< Etag: W/"84f-lZ8MJ+JUtmxxiomsdNc0g0Rg40I"
< Date: Wed, 05 Feb 2025 22:22:37 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Content-Length: 2127
< 
<!DOCTYPE html>
<html lang="en">
    <head>
      <script type="module">

Issues:

• Middleware isn’t logging, so it might not be running.
• curl returns the landing page instead of API response.

Questions:

• Any ideas why it's not working?
• How to get access to entity inside middleware?
Was this page helpful?