d. Example server index.js (both are the same implementation for each server1/2):
const app = express()
app.set("serverPort", process.env.SERVER_PORT)
app.use(helmet())
app.use(morgan("dev"))
app.use(express.json())
app.get("/api/v2/", (req, res) => {
res.send({ message: "Hello World v2"})
})
app.listen(app.get("serverPort"), () => {
console.log(`Express Server 2 Listening On Port ${app.get("serverPort")}`)
})
3. The problem I’m having:
1. going to https://localhost/api/v1 or /v2, site cant be reach
2. going to https://localhost:8080/ or anything other than endpoint proxy, display blank page (doesn't show 404.html)
3. going to https://localhost:8080/api/v1 or /v2, display json sent from server (but doesnt work without the port, site cant be reach)
Request matching in Caddy is exact, so this would only match requests to exactly /api/v1/ and nothing else, i.e. excluding /api/v1/foo.
To fix this, use /api/v1/* for your matcher instead. If you need to also match /api/v1, then you may use /api/v1* but be aware that this would also match /api/v11 as well for example. You can use a named matcher + the path matcher to do @v1 path /api/v1 /api/v1/*, to work around that.
Right, because if you specify a port, Caddy will only be listening on port 8080. That’s to be expected. If you configure Caddy with your site address as localhost instead of localhost:8080 then it will listen on port 443 for HTTPS requests (the default HTTPS port).
You used a / matcher, which only matches exactly / and nothing else. To match all requests not otherwise handled by your v1 or v2handle blocks, then use another handle block without a matcher, that will act as a catch-all.
So here’s what i’ve done to solve my problem. Instead of redirect, I rendered the 404.html file I created in the root directory when client make request other than the proxy endpoint i provided. By using the handle_errors directive. Here’s how I implement.
The reason behind usage of file_server directive is to hide the 404.html file and avoid client making request directly to https://localhost/404.html which will send a 404 instead of 200.
@francislavoie I wanted to ask something regarding my caddyfile configuration. The above config I did not specify any cors option. But why didn’t I get any cors related error? Sorry if this question not making any sense