-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
52 lines (41 loc) · 1.24 KB
/
main.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
48
49
50
51
52
'use strict';
const Hapi = require('@hapi/hapi');
const axios = require('axios');
const init = async () => {
const server = Hapi.server({
port: process.env.PORT || 5000,
});
await server.register({
plugin: require('hapi-cors'),
options: {
origins: ['*']
}
});
server.route({
method: ['GET'],
path: '/{any*}',
handler: async (request, h) => {
const url = request.url.href.replace(request.url.origin, '').substr(1,)
if (request.route.method == 'get') {
return await axios.get(url)
.then(response => {
const data = response.data
return h.response(data).code(200)
}).catch(error => {
const data = {
"statusCode": 404,
"message": error.Error
}
return h.response(data).code(404)
})
}
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();