Is it possible to use API routers in main.wasp?
Example of a router:
Then is it possible to do this in main.wasp:
3 Replies
Hi @unagi7171. 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 provided knowledge sources, it's not directly possible to use API routers in
main.wasp
in the way you've described. Wasp has its own way of defining API routes and doesn't support using Express routers directly in the main.wasp
file. However, there are alternative approaches you can use to achieve similar functionality within Wasp's framework. Let me explain:
1. Custom API Endpoints:
Wasp allows you to define custom API endpoints using the api
declaration in main.wasp
. However, these are individual endpoints rather than full routers. Here's an example of how you can define a custom API endpoint Custom HTTP API Endpoints (https://wasp-lang.dev/docs/advanced/apis):
2. Server Setup Function:
You can use the setupFn
in the server
configuration to add custom routes or middleware. This allows you to add Express routers to your Wasp app Server Config (https://wasp-lang.dev/docs/project/server-config). Here's an example:
In your myServerSetupCode.js
, you could then use your router:
3. API Namespaces:
Wasp supports apiNamespace
for applying middleware to a group of API routes under a specific path Customize Per-Path Middleware (https://wasp-lang.dev/docs/advanced/middleware-config#3-customize-per-path-middleware). However, this is used for middleware configuration rather than defining routes:
In conclusion, while you can't directly use Express routers in main.wasp
as shown in your examples, you can achieve similar functionality by using Wasp's api
declarations for individual endpoints and the setupFn
for more complex routing setups. The apiNamespace
feature is primarily for middleware configuration rather than route definition.
For the most flexible approach, I would recommend using the setupFn
method, as it allows you to use your Express routers within the Wasp framework.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @unagi7171, 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?@unagi7171 not right now unfortunately, at least not via "custom api" functionality, although if you wanted, you coudl add it to the app via
serverSetup
functionality I believe: https://wasp-lang.dev/docs/project/server-configServer Config | Wasp
You can configure the behavior of the server via the server field of app declaration:
Created an issue for it here, for us to track: https://github.com/wasp-lang/wasp/issues/2366
GitHub
Allow setting Express Router as the custom api endpoint? · Issue #2...
Right now, for custom api endpoint in Wasp, you need to provide a JS function that is Express middleware. If would be cool if you could also provide an Express Router, which is (in a way) a higher-...