I have a Node application that should run the frontend on one Docker container and the backend on another one. Both the frontend and backend are using Express. Currently I'm trying to redirect the APIs called from the client on the frontend to the backend.
Example: frontend running on 38.127.211.116 calls the api /api/get-price/toy/1 and this api should be forwarded to the backend running at 87.105.146.57.
I managed to to this using a proxy in the frontend server, I used a npm package called 'http-proxy-middleware'. At the moment my code looks like this (everthing under the path ?api is forwarded to the backend):
.use('/api', createProxyMiddleware({
target: process.env.NODE_ENV === "prod" ? "someValue" : https://${devIpAddress}:${process.env.BACKEND_PORT}/`,
// changeOrigin: true,
secure: process.env.NODE_ENV === "prod" ? true : false // is it safe to have false in prod?
}))
I have a few questions because I don't fully understand which are the implications of the different approaches:
Proxy vs CORS: Is it better this approach (proxy from frontend to backend) or a direct call from the frontend to the backend allowing CORS in the backend? Which are the advantages and disadvantages?
secure option in the proxy: I can read from the docs: 'set secure to true/false, if you want to verify the SSL Certs'. If I always use false, also in prod, what could be the disadvantage? Will I have security issues? If yes, which one? Because with a proxy my backend address is hidden(?) but if I had only one machine every user would be able to see and call all my APIs anyway so what's the point of validating the caller?
origin option in proxy: what is the advantage of enabling change origin?
Authentication: a few pages of the website will require user authentication. For the APIs I think that the process will be pretty straightforward because the call will be redirected to the backend and the response will be passed back with the correct response based on the user credentials. I'm more interested in the user auth handling in the frontend. Because I'll have to provide html pages and these pages should be provided ONLY to logged users.But the DB with the credentials is in the backend so what would be the best strategy here? I was thinking to place a middleware before the code that shares the public Express folder and to send a http request to the backend when a user is requesting a a page that requires login. If the response is positive then the request will continue showing the page continue otherwise it will exit. But I'm wondering if this is the right approach. Something like:
.use("/admin", ()=>{ // pseudo code const result = httpCallToBackend(); if(!result) exit; }) // if positive go to next instruction - show html page .use(express.static(process.env.STATIC_FOLDER_PATH, { extensions: ["html"] }))