-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
224 lines (193 loc) · 6.24 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const fs = require('fs');
const yenv = require('yenv');
if (fs.existsSync('./env.yaml')) {
process.env = yenv('env.yaml', { strict: false });
}
const path = require('path');
const express = require('express');
const historyApiFallback = require('connect-history-api-fallback');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const moment = require('moment');
const _ = require('lodash');
const MongoStore = require('connect-mongodb-session')(session);
const numeral = require('numeral');
const helmet = require('helmet');
const colors = require('colors');
const cron = require('node-cron');
const crypto = require('crypto');
const common = require('./lib/common');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('./webpack.config');
const { initDb, getDbUri } = require('./lib/db');
const { addSchemas } = require('./lib/schema');
const { runIndexing } = require('./lib/indexing');
let handlebars = require('express-handlebars');
const i18n = require('i18n');
// Validate a settings schema
const Ajv = require('ajv');
const ajv = new Ajv({ useDefaults: true });
// get config
const config = common.getConfig();
// require the routes
const admin = require('./routes/admin');
const customer = require('./routes/customer');
const product = require('./routes/product');
const indexRoute = require('./routes/index');
const sessionRoute = require('./routes/session');
// require routes
const app = express();
// session store
const store = new MongoStore({
uri: getDbUri(config.databaseConnectionString),
collection: 'sessions'
});
// Setups secret
if (!config.secretCookie || config.secretCookie === '') {
const randomString = crypto.randomBytes(20).toString('hex');
config.secretCookie = randomString;
common.updateConfigLocal({ secretCookie: randomString });
}
if (!config.secretSession || config.secretSession === '') {
const randomString = crypto.randomBytes(20).toString('hex');
config.secretSession = randomString;
common.updateConfigLocal({ secretSession: randomString });
}
app.enable('trust proxy');
app.use(helmet());
app.set('port', process.env.PORT || 3001);
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser(config.secretCookie));
app.use(session({
resave: false,
saveUninitialized: false,
secret: config.secretSession,
cookie: {
path: '/',
httpOnly: true,
maxAge: 900000
},
store: store
}));
app.use(bodyParser.json());
// serving static content
app.use(express.static(path.join(__dirname, 'public')));
// setup the routes
app.use('/', admin);
app.use('/', customer);
app.use('/', product);
app.use('/', sessionRoute);
app.use('/', indexRoute);
// catch 404 and forward to error handler
// app.use((req, res, next) => {
// const err = new Error('Not Found');
// err.status = 404;
// next(err);
// });
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
const compiler = webpack(webpackConfig);
app.use(historyApiFallback({
verbose: false
}));
app.use(webpackDevMiddleware(compiler, {
publicPath: webpackConfig.output.publicPath,
contentBase: path.resolve(__dirname, '../client/public'),
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false
}
}));
app.use(webpackHotMiddleware(compiler));
app.use(express.static(path.resolve(__dirname, '../dist')));
// development error handler
// will print stacktrace
app.use((err, req, res, next) => {
console.error(colors.red(err.stack));
res.status(err.status || 500);
// res.render('error', {
// message: err.message,
// error: err,
// helpers: handlebars.helpers
// });
});
} else {
app.use(express.static(path.resolve(__dirname, '../dist')));
app.get('*', function (req, res) {
res.sendFile(path.resolve(__dirname, '../dist/index.html'));
res.end();
});
}
// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => {
console.error(colors.red(err.stack));
res.status(err.status || 500);
// res.render('error', {
// message: err.message,
// error: {},
// helpers: handlebars.helpers
// });
});
app.on('uncaughtException', (err) => {
console.error(colors.red(err.stack));
process.exit(2);
});
initDb(config.databaseConnectionString, async (err, db) => {
// On connection error we display then exit
if (err) {
console.log(colors.red('Error connecting to MongoDB: ' + err));
process.exit(2);
}
// add db to app for routes
app.db = db;
app.config = config;
app.port = app.get('port');
// Fire up the cron job to clear temp held stock
cron.schedule('*/1 * * * *', async () => {
const validSessions = await db.sessions.find({}).toArray();
const validSessionIds = [];
_.forEach(validSessions, (value) => {
validSessionIds.push(value._id);
});
// Remove any invalid cart holds
await db.cart.deleteMany({
sessionId: { $nin: validSessionIds }
});
});
// Set trackStock for testing
if (process.env.NODE_ENV === 'test') {
config.trackStock = true;
}
// Process schemas
await addSchemas();
// We index when not in test env
if (process.env.NODE_ENV !== 'test') {
try {
await runIndexing(app);
} catch (ex) {
console.error(colors.red('Error setting up indexes:' + err));
}
}
// Start the app
try {
await app.listen(app.get('port'));
app.emit('appStarted');
if (process.env.NODE_ENV !== 'test') {
console.log(colors.green('MyCart running on host: http://localhost:' + app.get('port')));
}
} catch (ex) {
console.error(colors.red('Error starting MyCart app:' + err));
process.exit(2);
}
});