-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (69 loc) · 1.83 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
/**
* Module Dependencies
*/
const config = require('./config');
const express = require('express');
const path = require('path');
const logger = require('morgan');
const bodyParser = require('body-parser');
const winston = require('winston');
const auth = require('./middlewares/auth').isAuth;
/**
* Set Node Env Mode
*/
process.env.NODE_ENV = config.mode;
const index = require('./routes');
const api = require('./routes/api');
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
/**
* Authentication Middleware
*/
app.use('/api', auth);
/**
* Other Middleware
*/
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api', api);
app.use('/', index);
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = 404;
next(err);
});
/**
* Error Handlers
* Development Error Handler
*/
if ('development' === process.env.NODE_ENV) {
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({ message: err.message, error: err });
});
}
/**
* Production Error Handler
*/
app.use((err, req, res, next) => {
if (404 !== err.status) winston.log('error', err);
res.status(err.status || 500);
res.json({ error: true, message: err.message });
});
/**
* Application Listening On PORT
*/
app.listen(config.port, winston.log('info', `Node.js server is running at http://localhost:${config.port}
in ${process.env.NODE_ENV} mode with process id ${process.pid}`)
);
/**
* Checking Uncaught Exceptions
*/
process.on('uncaughtException', err => {
winston.log('error', (new Date()).toUTCString() + ' uncaughtException:', err.message);
winston.log('info', err.stack);
process.exit(1);
});