-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (59 loc) · 1.82 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
'use strict';
/**
* This server is used to mainly host the front-end of the web app.
*
* For anything other than accessing the web app is forwarded the api server.
**/
const express = require('express');
const request = require('request');
const compression = require('compression');
const favicon = require('serve-favicon');
const httpProxy = require('http-proxy');
const url = require('url');
const config = require('./config.json')
const apiServer = config.apiServer;
// localhost
var proxy = httpProxy.createProxyServer();
var app = express();
app.use(favicon(__dirname + '/img/favicon-bar-chart.ico'));
app.use(compression());
app.get('/', function(req, res){
var token = req.query.client_token;
if(token !== undefined){
request({
'url' : 'https://apps.mypurecloud.com/api/v2/session',
'method' : 'GET',
'headers' : {
'Authorization' : 'bearer ' + token
}
}, function(error, response, body){
if(error || response.statusCode == 200){
res.sendFile(__dirname + '/views/index.html')
}
else{
res.sendFile(__dirname + '/views/#.html');
}
});
}
else{
res.sendFile(__dirname + '/views/#.html');
}
});
app.use('/dist', express.static(__dirname + '/dist'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/img', express.static(__dirname + '/img'));
app.use('/locales', express.static(__dirname + '/locales'));
/**
* Forward all other requests to api server
*/
app.use('/api/*', function(req, res){
console.log(req);
var path = url.parse(req.originalUrl).pathname;
path = path.slice(4, path.length);
console.log(path);
proxy.web(req, res, { 'target' : apiServer + path});
});
var port = process.argv[2] || 8000;
app.listen(port, function(){
console.log('Server running on port ' + port + '...');
});