-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
72 lines (62 loc) · 2.08 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict'
const shoppingcartApiUrl = 'https://tst.api.transavia.com/shoppingcartapi/'
const shoppingcartApiKey = '### ENTER YOUR API KEY ###'
const shoppingcartUsername = '### ENTER YOUR USERNAME ###'
const flightOffersApiUrl = 'https://tst.api.transavia.com/v1'
const flightOffersApiKey = '### ENTER YOUR API KEY ###'
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const fetch = require('node-fetch')
app.use(express.static('public'))
app.use(bodyParser.json({ type: 'application/json' }))
app.all('/shoppingcartAPI', (req, res) => {
const url = shoppingcartApiUrl + req.query.path
const config = {
method: req.method,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': shoppingcartApiKey,
//'Ocp-Apim - Trace': true,
'x-Username': shoppingcartUsername,
shoppingCartId: req.headers.shoppingCartId,
},
body: JSON.stringify(req.body),
cache: 'default',
}
return fetch(url, config)
.then(response => response.json())
.then(json => {
res.send(json)
})
.catch(e => {
console.log(e)
res.status(200).send(JSON.stringify(e)) // proxy endpoint returns 200 but with error content in the body
})
})
app.get('/flightOffers', (req, res) => {
const url = flightOffersApiUrl + req.originalUrl
const config = {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
apiKey: flightOffersApiKey,
//'Ocp-Apim - Trace': true
},
cache: 'default',
}
return fetch(url, config)
.then(response => response.json())
.then(json => {
res.send(json)
})
.catch(e => {
console.log(e)
res.status(200).send(JSON.stringify(e)) // proxy endpoint returns 200 but with error content in the body
})
})
app.set('port', process.env.PORT || 3001)
var server = app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + server.address().port)
})