There is a simple Node-Express application. running on 8080 port in docker
var express = require('express');
var app = express();
app.get("/", function(req, res) {
res.send(`
<h1> Application written in Node js</h1>
<h3> Home page </h3>
`)
});
app.get("/info", function(req, res) {
res.send(`
<h1> Application written in Node js</h1>
<h3> Info page </h3>
`)
});
app.get("/api", function(req, res) {
res.send({success: 1})
});
const PORT = process.env.NODE_DOCKER_PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
I made this Traefik config. I want to redirect any incoming request /app
to my nodejs docker.
example:
localhost/node
should go to my express route/
localhost/node/info
should go to my express route/info
localhost/node/api
should go to my express route/api
version: '3.1'
services:
nodeapp:
build: ./node_app
container_name: nodeapp
labels:
- "traefik.enable=true"
- "traefik.http.routers.node-server.rule=Host(`localhost`) && PathPrefix(`/app`)"
- "traefik.http.services.node-server.loadbalancer.server.port=8080"
networks:
- proxy
reverse-proxy:
image: traefik:v2.10
command: --api.insecure=true --providers.docker
ports:
- "80:80"
- "8080:8080"
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock
networks:
- proxy
networks:
proxy:
I tried Traefik middleware like these:stripprefix.prefixes
and stripprefixregex.regex
, but still did not find any solution.
Please help. thanks in advance
Explanation of changes:
With this configuration, requests made to localhost/app will be redirected to your Node.js application at port 8080, and the /app prefix will be stripped before forwarding to your Node.js routes. For example: