nodejs-express-proxy/index.js

48 lines
929 B
JavaScript

const express = require("express");
const morgan = require("morgan");
const { createProxyMiddleware } = require("http-proxy-middleware");
// Create Express Server
const app = express();
// Configuration
const PORT = 3000;
const HOST = "localhost";
const API_SERVICE_URL = "https://api.github.com";
// Logging
app.use(morgan("dev"));
// Info GET endpoint
app.get("/info", (req, res, next) => {
res.send(
"This is a proxy service which proxies to Billing and Account APIs."
);
});
// Authorization
app.use(" ", (req, res, next) => {
if (req.headers.authorization) {
next();
} else {
res.sendStatus(403);
}
});
// Proxy endpoints
app.use(
"/fetch_json",
createProxyMiddleware({
target: API_SERVICE_URL,
changeOrigin: true,
pathRewrite: {
[`^/fetch_json`]: "",
},
})
);
// Start the Proxy
app.listen(PORT, HOST, () => {
console.log(`Starting Proxy at ${HOST}:${PORT}`);
});