forked from ivanchauma/mozdevz-redesign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
131 lines (109 loc) · 3.25 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
// Modulos da aplicacao...
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
flash = require('connect-flash'),
expressSession = require('express-session'),
methodOverride = require('method-override'),
multer = require('multer'),
mongoose = require('mongoose'),
fs = require('fs');
// Variavel representacional da aplicacao
var app = express();
app.locals.dbURL = require('./config/db').url;
// ======= Configuracoes da aplicacao ======== //
app.set('port', process.env.PORT || 3000); // Porta http onde estara disponivel a app
// Configuracoes das views
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// Descomentar a linha seguinte se existir favicon no directorio /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.png')));
app.use(logger('dev')); // Gestor de logs de erro na consola
// Parse dos corpos dos http requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false,
parameterLimit: 10000,
limit: 1024 * 1024 * 10 }));
// Adiciona suporte a verbos PUT e DELETE do HTTP
app.use(methodOverride(function(req, res){
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
var method = req.body._method
delete req.body._method
return method
}
}));
app.use(cookieParser()); // Manipulacao dos cookies nos requests
app.use(express.static(path.join(__dirname, 'public'))); // Directorio de ficheiros estaticos
// Controlo de sessoes no app
app.use(expressSession({
secret: 'moz forum app00',
saveUninitialized: false,
resave: true
,cookie:{_expires : app.locals.tempoMaximoSessao*60*1000
}
}));
// Flash message para express
app.use(flash());
// Rotas da aplicacao
var root = require('./routes/index')(app);
var users = require('./routes/users')(app);
app.use('/', root);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
var db = mongoose.connection;
db.on('error', function(){
console.log('Erro ao connectar-se com a BD: '+app.locals.dbURL);
process.exit(1);
});
db.once('open', function(){
console.log('Connectado com sucesso a BD: '+app.locals.dbURL);
app.listen(app.get('port'), function(){
console.log('Aplicacao iniciada na porta '+app.get('port'));
});
});
// Conexao com a Base de Dados
var options = {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
}
},
replset: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS : 30000
}
}
};
mongoose.connect(app.locals.dbURL, options);