-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
72 lines (53 loc) · 1.8 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
// Notes
// http methods
/*
In our Database In http
- Create INSERT post
- Read SELECT get
- Update UPDATE put/patch
- Destroy DELETE delete
*/
// HTTP is stateless
// MVC = model view controllers - separation of concerns
// resources you're working with - dishes, users, comments, views
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
const logger = require('./middlewares/logger');
const session = require('express-session');
const methodOverride = require('./middlewares/method_override');
// const expressLayouts = require('express-ejs-layouts');
const setCurrentUser = require('./middlewares/set_current_user');
const viewHelpers = require('./middlewares/view_helpers');
const dishController = require('./controllers/dish_controller');
const sessionController = require('./controllers/session_controller');
const userController = require('./controllers/user_controller');
app.set('view engine', 'ejs');
app.use(logger);
// parses the raw request body and turns it into an object accessible at req.body
app.use(express.urlencoded({extended: true}));
app.use(express.static('public'));
app.use(methodOverride);
// app.use(expressLayouts);
app.use(
session({
secret: process.env.SECRET_KEY || 'keyboard cat',
resave: false,
saveUninitialized: true,
})
);
// to get currentUser to be available in all my views
app.use(setCurrentUser);
app.use(viewHelpers);
app.get('/login', (req, res) => {
res.render('login');
});
app.use('/users', userController);
app.use('/sessions', sessionController);
app.use('/dishes', dishController);
app.all('*', (req, res) => {
res.redirect('/dishes');
});
app.listen(port, () => {
console.log(`the server is listening on port ${port}`);
});