-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (94 loc) · 2.82 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
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const exphbs = require('express-handlebars');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
// const LocalStrategy = require('passport-local').Strategy;
// const mongo = require('mongodb');
const mongoose = require('mongoose');
// const fs = require('fs');
// const db = mongoose.connection;
mongoose.connect('mongodb://localhost:27017/test');
const routes = require('./routes/index');
const users = require('./routes/users');
const playground = require('./routes/playground');
const profile = require('./routes/profile');
// Init Index
const app = express();
// const mongoDB = 'mongodb://localhost:27017/test';
app.use(express.static(path.join(__dirname, 'public')));
// View Engine
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({ defaultLayout: 'layout' }));
app.set('view engine', 'handlebars');
// BodyParser & CookieParser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false,
}));
app.use(cookieParser());
// serve the tree
// function getDirectories(dir) {
// return fs.readdirSync(dir).filter((file) => {
// return fs.statSync(dir, `${'/'} ${file}`).isDirectory();
// });
// }
// console.log(getDirectories(__dirname, '/public'));
// const getDirs = (rootDir, cb) => {
// fs.readdir(rootDir, (err, files) => {
// const dir = [];
// for (let index = 0; index < files.length; ++index) {
// const file = files[index];
// if (file[0] !== '.') {
//
// }
// }
// })
// }
// Express Session
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true,
}));
// Passport Init
app.use(passport.initialize());
app.use(passport.session());
// Express Validator
app.use(expressValidator({
errorFormatter: (param, msg, value) => {
const namespace = param.split('.');
const root = namespace.shift();
let formParam = root;
while (namespace.length) {
formParam += path.join(`${'['}`, namespace.shift(), `${']'}`);
}
return {
param: formParam,
msg,
value,
};
},
}));
// Connect flash
app.use(flash());
// Global Vars
app.use((req, res, next) => {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
next();
});
app.use('/', routes);
app.use('/users', users);
app.use('/playground', playground);
app.use('/profile', profile);
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'), () => {
console.log('Server started on port ', app.get('port'));
});