Proxying API Requests in Development #1494
Replies: 2 comments 6 replies
-
Take a look at e.g. this repo. It's meant to show an auth flow with express but don't worry about that. The important part is that it uses a custom integration with express instead of the built-in Remix App-Server. This part in /server/index.js: function handleRequest(req, res, next) {
let build = require('./build')
if (MODE !== 'production') {
purgeRequireCache()
}
if (requireAuthentication(req)) {
return unauthenticated(req, res)
}
return createRequestHandler({
build,
getLoadContext,
mode: MODE,
})(req, res, next)
} Since this is "just express", it should be easy to add something like http-proxy-middleware. For reference, this is Remix's official code for the express template's server. |
Beta Was this translation helpful? Give feedback.
-
I finally got around to playing with remix today and need the same functionality of being able to route I made a simple proxy to sit in front, then I made remix listen on port 4000, the proxy on port 3000. The proxy grabs all
An alternative was to use the express adapter and just build the proxy in. In that case, my server.js was similar to the default with just the addition was the createProxyMiddleware line above the
NOTE1: On both implementations, when I do a simple navigation to the route, it would show a not found page briefly and then show the actual proxied page. It seems the client-side navigation is triggered there? That is not ideal, but its only for dev mode so something I could live with. I ended up making a NOTE2: I'm not sure if there is an advantage to using the remix server vs the express adapter. I like that the express one is just baked in, so I don't have to deal with another "thing". But, is there an advantage to the remix server? Does it work "better" than the nodemon/express setup? NOTE3: I'm starting this particular project from scratch, so I'm not sure if I really will need this api proxy after all. If I use loaders and actions, then it might be entirely avoidable with a little redesign of some backend things. However, if I were trying to incrementally port a previous CRA w/ react-query or something, then this would def be needed. Did anyone else come up with any other ways, such as using a loader itself. I was getting fetch errors about certs and such when I tried that route a couple times. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey 👋
in Create-React-App it is easily possible to proxying API requests only in development mode. See: https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually
Is this somehow possible to do also wit remix?
Beta Was this translation helpful? Give feedback.
All reactions