-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpress-example.js
42 lines (37 loc) · 1.02 KB
/
express-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const request = require("request");
const express = require("express");
const app = express();
const port = 8080;
// https://stackoverflow.com/questions/10435407/proxy-with-express-js
const use = (path, uriBase) => {
app.use(path, (req, res, next) => {
var method, r;
method = req.method.toLowerCase().replace(/delete/, "del");
switch (method) {
case "get":
case "post":
case "del":
case "put":
const json = req.body;
const uri = uriBase + req.baseUrl;
r = request[method]({
uri: uri,
qs: req.query,
json: json
});
console.log(
req.baseUrl + " -> " + method + ": " + uri + " " + (json ? json : "")
);
break;
default:
return res.send("invalid method");
}
return req.pipe(r).pipe(res);
});
};
use("/api/*", "http://localhost:8081");
use("*", "http://localhost:8082");
app.listen(port, function() {
console.log("Express at port", port);
});
process.on("uncaughtException", err => {});