-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
130 lines (115 loc) · 3.41 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
118
119
120
121
122
123
124
125
126
127
128
129
130
// BASE SETUP
// ======================================
// CALL THE PACKAGES --------------------
var express = require('express'), // call express
compression = require('compression'),
helmet = require('helmet'),
config = require('./config'),
path = require('path'),
favicon = require('serve-favicon'), // set favicon
bodyParser = require('body-parser'),
colors = require('colors'),
logo = require('./printLogo'),
argv = require('yargs').argv,
ngrok = require('ngrok');
var app = express(); // define our app using express
var scribe = require('scribe-js')(); // used for logs
var console = require('./app/models/consoleService')
('MAIN PROCESS', ['magenta', 'inverse']);
var token = '';
var publicFolder = __dirname + (argv.dist ? '/public/dist' : '/public');
if (argv.dist) {
console.info('!!PRODUCTION!!');
}
// hook helmet to our express app. This adds some protection to each communication with the server
// read more at https://github.com/helmetjs/helmet
app.use(helmet());
// compress all requests
app.use(compression({
threshold: 0
}));
colors.enabled = true; //enable colors even through piping.
// create application/json parser
var jsonParser = bodyParser.json();
/** ===========
* = LOGGING =
* = =========
* set up logging framework in the app
* when NODE_ENV is set to development (like in gulp watch),
* don't log at all (TODO: make an exception for basic stuff
* like: listening on port: XXXX)
*/
if (process.env.NODE_ENV === 'development' || !argv.dist) {
// remove logging completely
/*var noop = function() {
return console;
};
var console = {
time: noop,
date: noop,
tag: noop,
t: noop,
file: noop,
f: noop,
info: noop,
log: noop,
error: noop,
warning: noop
};
process.console = console;*/
} else {
app.use(scribe.express.logger());
}
app.use('/logs', scribe.webPanel());
/** ================
* = STATIC FILES =
* = ==============
* set static files location used for requests that our frontend will make
*/
app.use(express.static(publicFolder));
/** =================
* = SERVE FAVICON =
* = ===============
* serve the favicon.ico so that modern browsers will show a "tab" and favorites icon
*/
app.use(favicon(path.join(__dirname,
'public', 'assets', 'images', 'favicon.ico')));
/** ==================
* = ROUTES FOR API =
* = ================
* set the routes for our server's API
*/
var apiRoutes = require('./app/routes/api')(app, express);
app.use('/api', jsonParser, apiRoutes);
/** =============
* = FRONT-END =
* = ===========
* Main 'catch-all' route to send users to frontend
*/
/* NOTE(thatkookooguy): has to be registered after API ROUTES */
app.get('*', function(req, res) {
res.sendFile(path.join(publicFolder + '/index.html'));
});
/** ==========
* = SERVER =
* = ========
*/
app.listen(config.port, function() {
logo();
console.info('Server listening at port ' +
colors.bgBlue.white.bold(' ' + config.port + ' '));
});
if (token) {
ngrok.authtoken(token, function(err, token) {
if (err) {
console.error(err);
}
});
ngrok.connect(config.port, function (err, url) {
if (err) {
console.error(err);
} else {
console.info(colors.cyan('ngrok') + ' - serving your site from ' + colors.yellow(url));
}
});
}