-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
117 lines (104 loc) · 2.98 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Dependencies.
*/
var Hapi = require('hapi'),
config = require('./server/config/settings'),
routes = require('./server/config/routes'),
Ejs = require('ejs'),
Wreck = require('wreck'),
util = require('util');
// FOR API SERVER:
var server = Hapi.createServer(config.server.listenHost, config.server.listenPort, {cors: true}); //, {
// Add the server routes
routes.init(server);
var notFound = function(request, reply) {
reply('The page was not found').code(404);
};
server.route({
method: '*',
path: '/{p*}',
handler: notFound
});
// FOR GUI SERVER
var API = {
call: function(opts) {
var url = 'http://' + config.server.listenHost + ':' + config.server.listenPort + opts.url;
var requestOptions = {
headers: { 'content-type':'application/json'}
};
// Add payload
if(opts.payload) {
requestOptions.payload = JSON.stringify(opts.payload);
}
if(opts.method === 'POST' || opts.method === 'post')
{
Wreck.post(url, requestOptions, opts.callback)
}
else if(opts.method === 'PUT' || opts.method === 'put')
{
Wreck.put(url, requestOptions, opts.callback)
}
else if(opts.method === 'DELETE' || opts.method === 'delete')
{
Wreck.delete(url, requestOptions, opts.callback)
}
else
{
Wreck.get(url, requestOptions, opts.callback);
}
}
};
server.route({
path: "/api/get{resourcename}Excel",
method: "GET",
handler: function(request, reply) {
API.call({
method: 'GET',
url: request.path.substring(4),
callback: function(err, res, payload) {
if (err) throw err;
return reply(payload).header('Content-Type', 'application/octet-stream').header('content-disposition', 'attachment; filename='+request.params.resourcename+'.csv;');
}
});
}
});
server.route({
path: "/api/{path*}",
method: ["GET","POST","PUT","DELETE"],
handler: function(request, reply) {
API.call({
method: request.method,
url: request.path.substring(4),
payload: request.payload,
callback: function(err, res, payload) {
if (err) throw err;
else
reply(payload);
}
});
}
});
server.route({
method: 'GET',
path: '/upload/{params*}',
handler: {
directory: { path: './upload' }
}
});
server.route({
method: 'GET',
path: '/{params*}',
handler: {
directory: { path: './client/app' }
}
});
server.route({
method: 'GET',
path: '/',
handler: {
file: "./client/app/index.html"
}
});
server.start(function () {
console.log('Server started ', server.info.uri);
});