-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·71 lines (62 loc) · 2.02 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
// app.js
var Server = require('http').Server;
var Express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var fs = require('fs');
var path = require('path');
var sizeOf = require('image-size');
var cors = require('cors');
var morgan = require('morgan');
// Load .env configuration
const envFilePath =
process.env.NODE_ENV === 'production' ? '.env' : '.env.development';
const envLoadResult = require('dotenv').config({ path: envFilePath });
// if (envLoadResult.error) {
// throw envLoadResult.error;
// }
// console.log(envLoadResult.parsed);
// Load StickyBoard config file
const stickyboardConfig = require('./stickyboard.config');
// Initialize the app
const app = new Express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(cors());
app.use(morgan('dev'));
// Configure support for ejs templates
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'src', 'views'));
// Initialize the server
const server = new Server(app);
// Define static assets folder
app.use('/static', Express.static(path.join(__dirname, 'src', 'static')));
app.use('/dist', Express.static(path.join(__dirname, 'dist')));
// Set port and running environment
const port = process.env.PORT || 3000;
const env = process.env.NODE_ENV || 'production';
delete process.env.BROWSER;
// Set app bundle URL depends on running environment
var appBundleUrl;
switch (env) {
case 'production':
appBundleUrl = '/dist/app.bundle.js';
break;
case 'development':
appBundleUrl = 'http://localhost:8080/app.bundle.js';
break;
}
app.get('*', function(req, res) {
res.render('index', { ...stickyboardConfig, APP_BUNDLE_URL: appBundleUrl });
});
// Start the server
server.listen(port, (err) => {
if (err) {
console.error(err);
setTimeout(startServer, 3000);
return;
}
console.info(`Server running on http://localhost:${port} [${env}]`);
});