-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (42 loc) · 1.15 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
46
47
const fs = require('fs');
const r = require('request'); //Required for Webpack
const rp = require('request-promise-native');
class ServerlessProxy {
constructor(app) {
this.app = app;
this.socket = '/tmp/serverless-proxy.sock';
if(fs.existsSync(this.socket)) {
fs.unlinkSync(this.socket);
}
app.listen(this.socket);
}
request(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
const options = {
uri: `http://unix:${this.socket}:${event.path}`,
qs: event.queryStringParameters,
headers: event.headers || {},
resolveWithFullResponse: true,
simple: false
};
rp(options)
.then(function (response) {
const successResponse = {
statusCode: response.statusCode,
body: response.body,
headers: response.headers
};
callback(null, successResponse);
})
.catch(function (err) {
console.error(err);
const errorResponse = {
statusCode: 502,
body: '',
headers: {}
};
callback(null, errorResponse);
});
}
}
module.exports = ServerlessProxy;