-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
executable file
·51 lines (44 loc) · 1.47 KB
/
proxy.js
File metadata and controls
executable file
·51 lines (44 loc) · 1.47 KB
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
46
47
48
49
50
51
#!/usr/bin/env node
var express = require('express');
var request = require('request');
var cors = require('cors');
var app = express();
app.use(cors());
var port = Number(process.env.PORT || 3000);
var apiServerHost = (process.env.ELASTIC_URL || 'http://127.0.0.1:9200')
// Listen for requests on all endpoints
app.use('/', function(req, res, body) {
// short-circuit favicon requests for easier debugging
if (req.url != '/favicon.ico') {
console.log('req.method: ' + req.method);
console.log('req.url: ' + req.url);
// Request method handling: exit if not GET or POST
if ( ! (req.method == 'GET' || req.method == 'POST') ) {
errMethod = { error: req.method + " request method is not supported. Use GET or POST." };
console.log("ERROR: " + req.method + " request method is not supported.");
res.write(JSON.stringify(errMethod));
res.end();
return;
}
// pass the request to elasticsearch
var url = apiServerHost + req.url;
req.pipe(request({
uri : url,
auth : {
user : 'username',
pass : 'password'
},
headers: {
'accept-encoding': 'none'
},
rejectUnauthorized : false,
}, function(err, res, body) {
// you could do something here before returning the response
})).pipe(res); // return the elasticsearch results to the user
}
});
// Server Listen
app.listen(port, function () {
console.log('App server is running on http://localhost:' + port);
console.log('apiServerHost: ' + apiServerHost);
});