Code Monkey home page Code Monkey logo

Comments (6)

0x80 avatar 0x80 commented on July 2, 2024

I have managed to get further with enabling ssl. Maybe that's a requirement if you call an https endpoint? I've fixed some other mistakes like hostname, so I am not sure what the issue was. I have managed to connect my proxy handler to a route in express. It still isn't returning any API response though.

The server code:

import * as express from "express";
import * as https from "https";
import * as fs from "fs";
import { join } from "path";
import flightstats from "./proxy/flightstats";

const app = express();
app.enable("trust proxy");
app.get("/", (req, res) => {
  res.send("Safe and secure!");
});

app.use("/api/flightstats", flightstats);

const key = fs.readFileSync(join(__dirname, "..", "ssl", "key.pem"));
const cert = fs.readFileSync(join(__dirname, "..", "ssl", "cert.pem"));

const server = https.createServer(
  {
    key,
    cert,
    passphrase: "something"
  },
  app
);

server.listen(8008);
console.log("Listening on 8008");

The ./proxy/flightstats code:

import { Router } from "express";
import * as proxy from "http2-proxy";

const router = Router();

router.get("/*", async (req, res) => {
  console.log(`Proxy clientRequest url: ${req.url}`);

  try {
    await proxy.web(req, res, {
      hostname: "api.flightstats.com",
      port: 443
    });
  } catch (err) {
    console.error(`Proxy failed: ${err.message}`);
  }
});

export default router;

Then I make a call with

$ curl -i --cacert ./ssl/cert.pem https://localhost:8008/api/flightstats/flex/airports/rest/v1/json/all\?appId\=1234\&appKey\=1234

And get the following response

HTTP/1.1 500 Internal Server Error
X-Powered-By: Express
Date: Mon, 08 Jan 2018 17:40:36 GMT
Connection: keep-alive
Transfer-Encoding: chunked`

And server console output:

Proxy clientRequest url: /flex/airports/rest/v1/json/all
Proxy failed: socket hang up

Any idea what might be going wrong here?

from node-http2-proxy.

ronag avatar ronag commented on July 2, 2024

onReq should return undefined or a HttpRequest also the second argument is not the res object.

I think what you want to do is:

    await proxy.web(req, res, {
      hostname: "https://api.flightstats.com",
      onReq: (req, { headers }) => {
        headers['x-forwarded-host'] = req.headers['host']
      }
    });

from node-http2-proxy.

ronag avatar ronag commented on July 2, 2024

And server console output:

Proxy clientRequest url: /flex/airports/rest/v1/json/all
Proxy failed: socket hang up
Any idea what might be going wrong here?

Could you try and verify a request directly against upstream? Could you give me a reproducible example so I can try it myself?

from node-http2-proxy.

0x80 avatar 0x80 commented on July 2, 2024

@ronag here you go https://github.com/0x80/http2-proxy-example

Thanks for looking into it.

from node-http2-proxy.

0x80 avatar 0x80 commented on July 2, 2024

Meanwhile I've come to the conclusion that the req.params in express are a result of parsing the url, so I probably shouldn't try to modify them. I suspect the right approach would be to rewrite the request path/url in order to append them. But I haven't managed to find out how to do that yet.

from node-http2-proxy.

ronag avatar ronag commented on July 2, 2024

Sorry for no response. Glad you figured it out.

from node-http2-proxy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.