-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
124 lines (89 loc) · 2.86 KB
/
server.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
const express = require('express');
const app = express();
const path = require('path');
const session = require('express-session');
const methodOverride = require('method-override');
require('dotenv').config();
console.log(process.env);
const firebase = require('./config/firebase');
//for input form parsing
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
const PORT = process.env.PORT || 3000;
// Express Middleware
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs'); // express will always look for ejs file views
// app.use(express.static(path.join(__dirname, 'public')));
//**Added functions to parse form data */
// for parsing application/json
app.use(express.json());
app.use(methodOverride('_method')); // must become before our routes for PUT/PATCH routes
// for parsing application/xwww-
app.use(express.urlencoded({ extended: true })); // nested properties in JSON objects can be accessed
// form-urlencoded
app.use(express.static(path.join(__dirname, 'public')));
// middleware for user session
app.use(session({
secret: 'pikachu secret',
resave: false,
saveUninitialized: false
}));
app.use((req, res, next) => {
res.locals.title = 'Pikaboo App';
res.locals.user = req.session.user;
next();
})
app.use((req, res, next) => {
res.locals.user = req.session.user;
next();
});
const pokemonsRoutes = require('./routes/pokemon');
app.use(pokemonsRoutes);
const locationsRoutes = require('./routes/location');
app.use(locationsRoutes);
const generationsRoutes = require('./routes/generations');
app.use(generationsRoutes);
const evolutionsRoutes = require('./routes/evolutions');
app.use(evolutionsRoutes);
const typesRoutes = require('./routes/types');
app.use(typesRoutes);
const mapsRoutes = require('./routes/maps');
app.use(mapsRoutes);
const itemsRoutes = require('./routes/items');
app.use(itemsRoutes);
const movesRoutes = require('./routes/moves');
app.use(movesRoutes);
const userRoutes = require('./routes/users');
app.use(userRoutes);
const demoRoutes = require('./routes/demo');
app.use(demoRoutes);
const authRoutes = require('./routes/auth');
app.use(authRoutes);
// for parsing multipart/form-data
app.use(upload.array());
app.get('/', (req, res) => {
res.render('index', {
title: 'Pikaboo Search App',
error: req.app.locals.err
});
});
app.get('/signin', (req, res) => {
if (req.session.user) {
return res.redirect(`/users/${req.session.user.uid}`);
}
res.render('signin/index', {
error: req.app.locals.err
});
});
app.get('/signup', (req, res) => {
if (req.session.user) {
return res.redirect(`/users/${req.session.user.uid}`);
}
res.render('signup/index', {
error: req.app.locals.err
});
})
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});