W
Wasp3mo 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"
}
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
}
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">
* 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?
2 Replies
kapa.ai
kapa.ai3mo ago
Hi @Alex. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, I can offer some insights into your issues with the apiNamespace in OpenSaaS. Let's address your questions and problems: 1. Why it's not working: The main issue seems to be that your API namespace configuration is not being applied correctly. There are a few potential reasons for this: a) Path configuration: Your apiNamespace is set to /api, but you're making a request to <http://localhost:3000/api/.> The trailing slash might be causing issues. Try removing it and make the request to <http://localhost:3000/api> instead. b) Port mismatch: You're making the request to port 3000, which is typically the client-side port in Wasp applications. The server usually runs on port 3001. Try changing your curl command to:
curl -v <http://localhost:3001/api>
curl -v <http://localhost:3001/api>
c) Middleware configuration: Your middleware configuration looks correct, but it's possible that it's not being applied properly. Make sure that your apiKeyAuth.ts file is in the correct location (typically src/server/middlewares/). 2. Getting access to entities inside middleware: To access entities inside your middleware, you need to modify your middleware function to accept a context parameter. Here's how you can do it:
import { MiddlewareConfigFn } from 'wasp/server'

export const apiKeyAuthMiddlewareConfigFn: MiddlewareConfigFn = (middlewareConfig) => {
middlewareConfig.set('custom.middleware', (req, res, next, context) => {
console.log('Middleware triggered')
// Access entities through context.entities
// For example: context.entities.YourEntity
next()
})
return middlewareConfig
}
import { MiddlewareConfigFn } from 'wasp/server'

export const apiKeyAuthMiddlewareConfigFn: MiddlewareConfigFn = (middlewareConfig) => {
middlewareConfig.set('custom.middleware', (req, res, next, context) => {
console.log('Middleware triggered')
// Access entities through context.entities
// For example: context.entities.YourEntity
next()
})
return middlewareConfig
}
To further debug your issue, I recommend the following steps: 1. Double-check that you're making the request to the correct port (3001 for the server). 2. Ensure that your apiNamespace configuration is in the correct place in your main.wasp file. 3. Try adding a more specific path to your apiNamespace, for example:
apiNamespace api {
middlewareConfigFn: import { apiKeyAuthMiddlewareConfigFn } from "@src/server/middlewares/apiKeyAuth",
path: "/api/v1"
}
apiNamespace api {
middlewareConfigFn: import { apiKeyAuthMiddlewareConfigFn } from "@src/server/middlewares/apiKeyAuth",
path: "/api/v1"
}
Then make your request to <http://localhost:3001/api/v1.> 4. Check your server console for any error messages that might provide more information about why the middleware isn't being applied. If you're still having issues after trying these suggestions, it might be helpful to create a minimal reproducible example and share it with the Wasp community for further assistance. For more information on configuring middleware in Wasp, you can refer to the Configuring Middleware documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Alex, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Alex
AlexOP3mo ago
ohh, my. the port was an issue

Did you find this page helpful?