-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
44 lines (37 loc) · 1.4 KB
/
server.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
const express = require('express');
const fetch = require('node-fetch');
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));
/* ------------------- fetches access token from amadeus -------------------- */
function fetchAmadeusAccessToken(req, res) {
const amadeusRequestAccessTokenBody = 'grant_type=client_credentials&client_id=' + process.env.AMADEUS_API_KEY + '&client_secret=' + process.env.AMADEUS_API_SECRET;
fetch('https://test.api.amadeus.com/v1/security/oauth2/token', {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: amadeusRequestAccessTokenBody
})
.then(function (response) {
return response.json();
})
.then(function (data) {
var accessToken = (data.access_token);
res.json(accessToken);
})
.catch(function (error) {
console.log(error);
});
};
// api get request from flights search to get access token from amadeus
app.get('/api/amadeus-access-token', fetchAmadeusAccessToken);
/* ------------------- sends rapid api key to frontend -------------------- */
app.get('/api/rapid-api-key', (req, res) => {
const rapidApiKey = process.env.RAPID_API_KEY;
res.json(rapidApiKey);
});
app.listen(port, () => console.log(`App listening on port ${port}`));