How to add extra routes to the Vite HTTP server? (serve front and back end at the same time) #20025
-
I have a React frontend running fine in Vite using the However the code makes API calls, and I'd like to host the API in Vite as well, to get away from our current process where we need to launch the React web site and the API server in two separate processes. Is there a way I can tell Vite that for a given URL path, the request should be passed to a different file in my codebase? My API code ultimately runs on AWS Lambda, so I have already written some shim code to convert an ExpressJS request into a Lambda-compatible call (so I can run the same code locally or on Lambda) so I don't mind modifying it to make it more Vite-compatible as it's only used for local development. However I am not sure how to configure Vite to call that code when a certain URL is requested. There are a few plugins that claim to do this but none of them quite work:
I don't need anything complicated, just any requests for a specific path on the exiting Vite HTTP server should get passed to a Typescript function, which lives in a different file to the React site. The React site handled by Is this sort of set up possible in Vite? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I ended up looking into plugins and found a quick simple solution:
This allows me to handle that one custom URL, but leave everything else to React. I extended this to handle the Lambda stuff, like so:
|
Beta Was this translation helpful? Give feedback.
I ended up looking into plugins and found a quick simple solution:
This allows me to handle that one custom URL, but leave everything else to React.
I extended this to handle the Lambda stuff, like so: