Comments (6)
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.
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.
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.
@ronag here you go https://github.com/0x80/http2-proxy-example
Thanks for looking into it.
from node-http2-proxy.
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.
Sorry for no response. Glad you figured it out.
from node-http2-proxy.
Related Issues (20)
- Regex bug by choosing agent HOT 1
- onReq with finalhandler = Error HOT 5
- onReq param 2 (options) no longer the same thing in 5.x? HOT 2
- unknown difference between 4.2.15 and 5.0.13 HOT 7
- websockets don't proxy with 5.0.34 if the path isn't the same unless onReq is used HOT 2
- path: Target pathname. Defaults to req.originalUrl || req.url. HOT 1
- bug: onReq HOT 2
- Forwarding to unix socket HOT 4
- add typescript support HOT 3
- Add "ca", "key", "cert" options and "rejectUnauthorized" options for TLS-based HTTP servers using self-signed certificates. HOT 10
- Adding "http2 to http2" proxy functions for proxy servers is now much easier. HOT 8
- add undici support
- Cluster support (Bug??) HOT 17
- Possible wrong type for onReq HOT 3
- Issue with custom response streams handler HOT 2
- unable to proxy http2 request
- Connection upgrade support only websocket HOT 1
- secure: false HOT 2
- dear sir ,http2 bug & please update your doc
- websocket proxy not completing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-http2-proxy.