-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (34 loc) · 1.13 KB
/
index.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
43
44
45
//@ts-check
// Simple proxy : works locally, when tested on google shell, but not when called from outside, may be because of redirections
//load the environment with the correct API Key, stored in env.yaml locally
require('yamlenv').config();
const CONFIG = {
apiKey: process.env.API_KEY,
upstream: process.env.UPSTREAMSERVER,
};
//now start creating the proxy
const express = require('express');
const morgan = require("morgan");
const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware');
//options for the proxy
const options = {
target: CONFIG.upstream,
changeOrigin: true, // needed for virtual hosted sites
// very important for POST request on google cloud
onProxyReq: fixRequestBody,
//logger: console,
//logLevel: "debug"
};
// create the proxy, the server
const aProxy = createProxyMiddleware(options);
const app = express();
//create a logger entry
morgan.token('req-headers', function(req,res){
return JSON.stringify(req.headers)
})
app.use(morgan(':method :url :status :req-headers '));
// mount the proxy in web server
app.use('/', aProxy);
module.exports = {
proxy: app
}