microservices/auth-service/routes/api.js

26 lines
693 B
JavaScript
Raw Normal View History

2025-05-20 17:03:18 -07:00
const { createProxyMiddleware } = require("http-proxy-middleware");
const api = require("express").Router();
const ASSIGNMENT_SERVICE_URL = process.env.ASSIGNMENT_SERVICE_URL;
api.use(
'/',
2025-05-20 17:03:18 -07:00
createProxyMiddleware({
target: ASSIGNMENT_SERVICE_URL,
changeOrigin: true,
logLevel: 'debug',
2025-05-20 17:03:18 -07:00
pathRewrite: {
'^/api': '', // remove "/api" from the start
},
onProxyReq(proxyReq, req, res) {
console.log(`Proxying request to: ${ASSIGNMENT_SERVICE_URL}${req.url}`);
2025-05-20 17:03:18 -07:00
},
onError(err, req, res) {
console.error('Proxy error:', err.message);
res.status(502).send('Bad Gateway: Failed to connect to target');
}
2025-05-20 17:03:18 -07:00
})
);
module.exports = api;