-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (57 loc) · 2.05 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
// Entry point for the app
// Express is the underlying that atlassian-connect-express uses:
// https://expressjs.com
import express from 'express';
// https://expressjs.com/en/guide/using-middleware.html
import bodyParser from 'body-parser';
import compression from 'compression';
import cookieParser from 'cookie-parser';
import errorHandler from 'errorhandler';
import morgan from 'morgan';
// atlassian-connect-express also provides a middleware
import ace from 'atlassian-connect-express';
// Use Handlebars as view engine:
// https://npmjs.org/package/express-hbs
// http://handlebarsjs.com
import hbs from 'express-hbs';
// We also need a few stock Node modules
import http from 'http';
import path from 'path';
import os from 'os';
// Routes live here; this is the C in MVC
import routes from './routes';
// Bootstrap Express and atlassian-connect-express
const app = express();
const addon = ace(app);
// See config.json
const port = addon.config.port();
app.set('port', port);
// Configure Handlebars
const viewsDir = __dirname + '/views';
app.engine('hbs', hbs.express4({partialsDir: viewsDir}));
app.set('view engine', 'hbs');
app.set('views', viewsDir);
// Log requests, using an appropriate formatter by env
const devEnv = app.get('env') == 'development';
app.use(morgan(devEnv ? 'dev' : 'combined'));
// Include request parsers
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
// Gzip responses when appropriate
app.use(compression());
// Include atlassian-connect-express middleware
app.use(addon.middleware());
// Mount the static files directory
const staticDir = path.join(__dirname, 'react-ui', 'dist');
app.use(express.static(staticDir));
// Show nicer errors in dev mode
if (devEnv) app.use(errorHandler());
// Wire up routes
routes(app, addon);
// Boot the HTTP server
http.createServer(app).listen(port, () => {
console.log('App server running at http://' + os.hostname() + ':' + port);
// Enables auto registration/de-registration of app into a host in dev mode
if (devEnv) addon.register();
});