-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
188 lines (157 loc) · 4.74 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
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
const express = require('express')
/* CORS */
const cors = require('cors')
/* Body Parser */
const bodyParser = require('body-parser');
const multer = require('multer');
/* Logger */
const logger = require('morgan');
const winston = require('winston');
/* Utils */
const path = require('path')
const favicon = require('serve-favicon')
const compression = require('compression')
/* Session & Cookies */
const cookieParser = require('cookie-parser')
const session = require('express-session');
/* method-override: Override HTTP verbs. */
var methodOverride = require('method-override')
// Application
const app = express()
// Console
var colors = require('colors');
/**
* Template Engine (EasyTemplatJS)
*/
var fs = require('fs')
var Et = require('easytemplatejs');
Et.enableScript = true; // enable <etj-script>
Et.enableStyle = true; // enable <etj-style>
//var cache = true; // Use Cache?
var cache = process.env.NODE_ENV == 'production'; // Use Cache?
var cacheTpl = {};
app.engine('etj', function(filePath, data, callback) {
fs.readFile(filePath, function(err, content) {
if(err) return callback(err)
var compiled;
if(cache) {
if(!cacheTpl[filePath]) {
cacheTpl[filePath] = Et.template(content);
}
compiled = cacheTpl[filePath];
} else {
compiled = Et.template(content);
}
var rendered = compiled(data);
return callback(null, rendered)
})
})
app.set('views', './views')
app.set('view engine', 'etj')
// Logging
winston.level = 'debug'; // Winston logging level: error, warn, info, verbose, debug
app.use(logger('dev')); // Morgan logging level: combined, common, dev, short, tiny
app.use(compression()) // Compress
app.use(cors()) // CORS
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))) // favicon
app.use(cookieParser()) // Cookies
app.use('/static',express.static('public')) // static
// Memory Session temporary
/*
Warning: connect.session() MemoryStore is not
designed for a production environment, as it will leak
memory, and will not scale past a single process.
!!!Please use Redis Session presists!!!
*/
app.use(session({
secret: 'se3r4t',
name: 'nsessionId',
resave: true,
rolling: true,
saveUninitialized: true,
cookie: {
httpOnly: true,
maxAge: 1000*60*30
}
}));
// Redis Session presists
/*
const RedisStore = require('connect-redis')(session);
app.use(session({
store: new RedisStore({
host:'127.0.0.1',
port:6379
}),
secret: 'se3r4t',
resave: true,
rolling: true,
saveUninitialized: false,
cookie: {
httpOnly: true,
maxAge: 1000*60*30 // Session MaxAge(millisecond), Default is 24 Hours
}
}));
*/
/* Body Parser */
app.use(bodyParser.json({type: 'application/*+json'}))
app.use(bodyParser.raw({type: 'application/vnd.custom-type'}))
app.use(bodyParser.text({type: 'text/html'}))
app.use(bodyParser.urlencoded({ extended: false })); // create application/x-www-form-urlencoded parser
app.use(bodyParser.json()); // create application/json parser
/* Override HTTP verbs. */
// Support HTTP verbs request in form POST parmas `_method`
app.use(methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
// look in urlencoded POST bodies and delete it
var method = req.body._method
delete req.body._method
return method
}
}))
/* Router */
var index = require('./routes/index');
var users = require('./routes/users');
app.use('/', index);
app.use('/users', users);
/* Exception Handler*/
// 404
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// Server Exception
app.use(function(err, req, res, next) {
if(res.headersSent) {
return next(err)
}
res.locals.msg = err.message;
res.locals.err = process.env.NODE_ENV != 'production' ? err : {};
err.status = err.status || 500;
res.status(err.status);
res.render('error');
});
/**
* Start Server
*/
const PORT=3000;
app.listen(PORT, function() {
console.log('')
console.log(`=================================================`.yellow)
console.log(`Express quickstart app listening on port `+`${PORT}`.bgGreen+'!')
console.log('')
console.log(`process.env.NODE_ENV: `.cyan+(process.env.NODE_ENV||"").bold.bgGreen);
console.log(`EasyTemplate Engine cache: `.cyan+(cache?'enabled':'disabled').bold.bgGreen);
console.log('')
console.log(`http://127.0.0.1:3000`.green)
console.log(`=================================================`.yellow)
console.log('')
})
// If you don't use a process manager(like 'PM2''), this can prevent server crashes
//process.on('uncaughtException', function (err) {
// console.log('Caught exception: ', err);
//});
// Promise unhandled exception
process.on('unhandledRejection', (reason, p) => {
console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason);
});