-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
172 lines (129 loc) · 3.96 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
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var engine = require('ejs-mate');
var flash = require('connect-flash');
var validator = require('express-validator');
var passport = require('passport');
var localStrategy = require('passport-local').Strategy;
var User = require('./models/user');
/*
* Database connectivity with mongoose -- START
*/
// express-session - Module
var session = require('express-session');
var mongoose = require('mongoose');
var mongoStore = require('connect-mongo')(session);
mongoose.connect("mongodb://localhost:27017/cloneNetwork");
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connection open");
});
require('./secure/passport');
/*
* Database connectivity with mongoose -- END
*/
var app = express();
app.set('port', process.env.PORT || 3000 );
// view engine setup
// app.set('views', path.join(__dirname, 'views'));
app.engine('ejs',engine);
app.set('view engine', 'ejs');
/*
* Session storage in DB -- START
*/
app.use(session({
secret: '6sd65d6f45e95',
resave: true, // force session to resave itself even if it is mnot modified
saveUninitialized: false, // should be false for login session (used for new unmodified session)
store : new mongoStore({mongooseConnection:db})
}));
/*
* Session storage in DB -- END
*/
app.use(passport.initialize());
app.use(passport.session());
/*
* Express Validator - Error Formatter Middleware - START
*(the formParam value is going to get morphed into form body format useful for printing.)
*/
app.use(validator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
},
customValidators: {
isExist_email : function(email){
return new Promise(function(resolve,reject){
User.findOne({'email':email},(err,user) => {
if(err) throw err;
if(user){
return reject();
};
return resolve();
});
});
}
}
}));
/*
* Express Validator - Error Formatter Middleware - END
*/
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(flash());
app.use(function(req, res, next) {
res.locals.error = req.flash('error');
res.locals.success = req.flash('success');
if(req.user) res.locals.session_id = req.user._id;
next();
});
// route Path
var index = require('./routes/index');
var users = require('./routes/users');
var search = require('./routes/search');
var token = require('./routes/tokens');
var post = require('./routes/post');
app.use('/', index);
app.use('/', users);
app.use('/', search);
app.use('/', token);
app.use('/', post);
// 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 handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error',{title:'Page404',header:false});
});
app.listen(app.get('port'),function(){
console.log("Server is running on port "+app.get('port'));
})
module.exports = app;