forked from kn9ts/project-mulla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (74 loc) · 2.82 KB
/
index.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
'use strict';
require('./environment');
const express = require('express');
const app = express();
const path = require('path');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const routes = require('./server/routes');
const genTransactionPassword = require('./server/utils/genTransactionPassword');
const apiVersion = process.env.API_VERSION;
// view engine setup
app.set('views', path.join(__dirname, 'server/views'));
app.set('view engine', 'jade');
// trust proxy if it's being served in GoogleAppEngine
if ('GAE_APPENGINE_HOSTNAME' in process.env) app.set('trust_proxy', 1);
// Uncomment this for Morgan to intercept all Error instantiations
// For now, they churned out via a JSON response
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// not using express less
// app.use(require('less-middleware')(path.join(__dirname, 'server/public')));
app.use(express.static(path.join(__dirname, './server/public')));
// memory based session
app.use(session({
secret: process.env.SESSION_SECRET_KEY,
resave: false,
saveUninitialized: true,
}));
// on payment transaction requests,
// generate and password to req object
app.use(`/api/v${apiVersion}/payment*`, genTransactionPassword);
// get an instance of the router for api routes
const apiRouter = express.Router;
app.use(`/api/v${apiVersion}`, routes(apiRouter()));
app.all('/*', (req, res) => {
res.render('index', { title: 'Project Mulla' });
});
// use this prettify the error stack string into an array of stack traces
const prettifyStackTrace = stackTrace => stackTrace.replace(/\s{2,}/g, ' ').trim();
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.statusCode = 404;
next(err);
});
// error handlers
app.use((err, req, res, next) => {
if (typeof err === 'undefined') next();
console.log('An error occured: ', err.message);
const errorResponse = {
status_code: err.statusCode,
request_url: req.originalUrl,
message: err.message,
};
// Only send back the error stack if it's on development mode
if (process.env.NODE_ENV === 'development') {
const stack = err.stack.split(/\n/).map(prettifyStackTrace);
errorResponse.stack_trace = stack;
}
return res.status(err.statusCode || 500).json();
});
const server = app.listen(process.env.PORT || 8080, () => {
console.log('Your secret session key is: ' + process.env.SESSION_SECRET_KEY);
console.log('Express server listening on %d, in %s' +
' mode', server.address().port, app.get('env'));
});
// expose app
module.exports = app;