-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·101 lines (84 loc) · 2.82 KB
/
app.js
File metadata and controls
executable file
·101 lines (84 loc) · 2.82 KB
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
/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const debug = require('debug')('wchbotserver:app');
const express = require('express');
const bodyParser = require('body-parser');
// const syncRoutes = require('./routes/sync');
const path = require('path');
const bots = require('./lib/bots');
// create a new express server
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
next();
});
debug('Configuring bots...');
let initPromise = bots(app)
.then(config => {
debug('Configuring bots finished');
app.set('appEnv', config.appEnv);
debug(
`Starting Server...
Environment: ${(app.get('appEnv').isLocal) ? "local" : "bluemix"}`
);
debug('Configuring sync...');
// app.use('/sync', syncRoutes);
debug('Configuring sync finished');
debug('Configuring error handling...');
/* Catch 404 and forward to error handler */
app.use(function(req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
/* Error Handlers */
// Log unhandled Promise expections
process.on('unhandledRejection', (reason, p) => {
debug('Unhandled Rejection at: %o reason: %s', p, reason);
});
let errorHandler;
if (app.get('appEnv').isLocal === true) {
// Error Handler: Development
errorHandler = function(err, req, res, next) {
debug('Error handler %o', err);
res.status(err.status || 500);
res.send({
message: err.message,
error: err
});
};
}
else {
// Error Handler: Production
errorHandler = function(err, req, res, next) {
debug('Error handler %o', err);
res.status(err.status || 500);
res.send({
message: err.message,
error: {}
});
};
}
app.use('/', errorHandler);
debug('Configuring error handling finished');
})
.catch(err => debug(err));
module.exports = initPromise.then(() => app);