-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (66 loc) · 2.54 KB
/
app.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
/**
* Copyright 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
// ===== MODULES ===============================================================
import bodyParser from 'body-parser';
import cookieParser from 'cookie-parser';
import express from 'express';
import favicon from 'serve-favicon';
import logger from 'morgan';
import path from 'path';
// ===== MESSENGER =============================================================
import ThreadSetup from './messenger-api-helpers/thread-setup';
// ===== ROUTES ================================================================
import index from './routes/index';
import users from './routes/users';
import webhooks from './routes/webhooks';
const app = express();
/* =============================================
= Basic Configuration =
============================================= */
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
/* ---------- Views ---------- */
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
/* ---------- Static Assets ---------- */
app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
/* ---------- Parsers ---------- */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
/* ---------- Loggers &c ---------- */
app.use(logger('dev'));
/* =============================================
= Routes =
============================================= */
/* ---------- Primary / Happy Path ---------- */
app.use('/', index);
app.use('/webhook', webhooks);
app.use('/users', users);
/* ---------- Errors ---------- */
// catch 404 and forward to error handler
app.use(function(req, res, next) {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
app.use(function(err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
/* ---------- Messenger setup ---------- */
ThreadSetup.setGetStarted();
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port')); // eslint-disable-line
});
export default app;