Welcome to the Linux Foundation Forum!

Lesson 8 Express Version of single-route, multi-origin-proxy

const express = require("express");
const app = express();
const port = 3000;
const createError = require("http-errors");
const { createProxyMiddleware } = require("http-proxy-middleware");

function isUrl(req, res, next) {
  const { url } = req.query;
  try {
    const newURL = new URL(url);
    const targetURL = newURL.origin;
    req.test = { targetURL };
    next();
  } catch (err) {
    next(createError(400));
  }
}

app.use(
  "/",
  isUrl,
  (req, res, next) => {
    console.log("targetURL: ", req.test.targetURL);
    next();
  },
  createProxyMiddleware({
    target: "http://localhost:5000",
    // target: req.test.targetURL,
    changeOrigin: true,
  })
);

// 404 handling
app.use((req, res, next) => {
  next(createError(404));
});

// error handling
app.use((err, req, res, next) => {
  res.status(err.status || 500);
  res.send(err.message);
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

The target is hard-coded with target: "http://localhost:5000".

;) Is there a possibility to make the target dynamic (and use the express).

o:) Could you please provide an express example (instead of linking to medium.com version of a proxy with express)?

Comments

  • @dominik.urban if you wanted to make the target dynamic you can set an environment variable and take the port from that

    as for implementing in express - that's up to you. The approach of the material is focused on concepts and understanding, not providing boilerplate.

  • @davidmarkclements

    Could you please revisit my question how to make the target url dynamic. The question is about the whole url given via query params, and not by providing env variables.

    Or can I make the impl dynamic by using router instead of target (provided by http-proxy-middleware.

    Would be great to have a working solution in express instead of relying on fastify and the fastify-reply-from package.

    My question is not boilerplate-related. I am curious if there is a solution working in express - or is it conceptual not possible?

  • hey @dominik.urban ok I see what you're saying.

    Target here is used in the same way as base in fastify-reply-from - it's supposed to set the top level domain. We don't proxy to different targets/bases in Chapter 8, the "Single-origin, multi-route proxy" subsection doesn't deal with switching the base.

    Implementing the equivalent of the source material in Express mostly involves setting target to https://news.ycombinator.com/

    I think if you want to redirect to multiple targets, the router option, as you suggest, is the best way to go.

Categories

Upcoming Training