Skip to content

Commit 7191b48

Browse files
author
codingChewie
committed
Modular Routes
1 parent 7be4ab1 commit 7191b48

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

app.js

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const express = require('express')
2-
const eyes = require('eyespect')
32
const cookieParser = require('cookie-parser')
43

54
const app = express()
@@ -9,6 +8,12 @@ app.use(cookieParser())
98

109
app.set('view engine', 'pug')
1110

11+
const mainRoutes = require('./routes/index')
12+
const cardRoutes = require('./routes/cards')
13+
14+
app.use(mainRoutes)
15+
app.use('/cards', cardRoutes)
16+
1217
// app.use((req, res, next) => {
1318
// const err = new Error('Oh no')
1419
// err.status = 500
@@ -20,40 +25,6 @@ app.use((req, res, next) => {
2025
next()
2126
})
2227

23-
app.get('/', (req, res) => {
24-
const name = req.cookies.username
25-
26-
if (name) return res.render('index', { name })
27-
28-
res.redirect('/hello')
29-
})
30-
31-
app.get('/cards', (req, res) => {
32-
eyes.inspect(res.locals, 'res.locals')
33-
34-
res.render('card', {
35-
prompt: `Who is buried in Grant's tomb?`,
36-
})
37-
})
38-
39-
app.get('/hello', (req, res) => {
40-
const name = req.cookies.username
41-
42-
if (name) return res.redirect('/')
43-
44-
res.render('hello')
45-
})
46-
47-
app.post('/hello', (req, res) => {
48-
res.cookie('username', req.body.username)
49-
res.redirect('/')
50-
})
51-
52-
app.post('/goodbye', (req, res) => {
53-
res.clearCookie('username')
54-
res.redirect('/hello')
55-
})
56-
5728
app.use((req, res, next) => {
5829
const err = new Error('Not Found')
5930
err.status = 404

routes/cards.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const express = require('express')
2+
const router = express.Router()
3+
const eyes = require('eyespect')
4+
5+
router.get('/', (req, res) => {
6+
eyes.inspect(res.locals, 'res.locals')
7+
8+
res.render('card', {
9+
prompt: `Who is buried in Grant's tomb?`,
10+
})
11+
})
12+
13+
module.exports = router

routes/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const express = require('express')
2+
const router = express.Router()
3+
4+
router.get('/', (req, res) => {
5+
const name = req.cookies.username
6+
7+
if (name) return res.render('index', { name })
8+
9+
res.redirect('/hello')
10+
})
11+
12+
router.get('/hello', (req, res) => {
13+
const name = req.cookies.username
14+
15+
if (name) return res.redirect('/')
16+
17+
res.render('hello')
18+
})
19+
20+
router.post('/hello', (req, res) => {
21+
res.cookie('username', req.body.username)
22+
res.redirect('/')
23+
})
24+
25+
router.post('/goodbye', (req, res) => {
26+
res.clearCookie('username')
27+
res.redirect('/hello')
28+
})
29+
30+
module.exports = router

0 commit comments

Comments
 (0)