Created
August 8, 2019 19:08
-
-
Save NimaBoscarino/6810a1f55a2bc49db2072080430072ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const dogRoutes = express.Router() | |
const dogs = { | |
1: { | |
name: 'Jack' | |
}, | |
2: { | |
name: 'Freddy' | |
}, | |
3: { | |
name: 'Olive' | |
} | |
} | |
const chewToys = { | |
1: ['bone', 'squeaker', 'flip flops'], | |
2: ['stuffed duck', 'chair', 'hand'], | |
3: ['sticks', 'barbie doll', 'furniture'], | |
} | |
dogRoutes.get('/', (req, res) => { | |
res.json(dogs) | |
}) | |
dogRoutes.get('/:dogId', (req, res) => { | |
const theOneDog = dogs[req.params.dogId] | |
res.json(theOneDog) | |
}) | |
dogRoutes.get('/:dogId/chewToys', (req, res) => { | |
const dogsChewToys = chewToys[req.params.dogId] | |
res.json(dogsChewToys) | |
}) | |
module.exports = dogRoutes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const morgan = require('morgan') | |
const cookieParser = require('cookie-parser') | |
const app = express() | |
const dogRoutes = require('./routes/dogRoutes') | |
const PORT = 3000 | |
app.use(cookieParser()) | |
app.use(morgan('dev')) | |
app.use((req, res, next) => { | |
res.cookie('tag', "you're it!") | |
next() | |
}) | |
app.use('/dogs', (req, res, next) => { | |
// check to see if the person is logged in | |
console.log(req.cookies) | |
// if not logged in, redirect to home page | |
if (!req.cookies.name) { | |
res.redirect('/') | |
} else { | |
// if they are, continue | |
next() | |
} | |
}) | |
// app.use((req, res, next) => { | |
// console.log('woo!') | |
// next() | |
// }) | |
// let counter = 1 | |
// app.use((req, res, next) => { | |
// console.log(counter++) | |
// next() | |
// }) | |
// app.use((req, res, next) => { | |
// console.log('Someone made a ', req.method, 'to', req.url) | |
// next() | |
// }) | |
// app.use('/hello', (req, res, next) => { | |
// console.log('SOMEONE HELLOED') | |
// next() | |
// }) | |
app.get('/', (req, res) => { | |
res.send('welcome to the main page!') | |
}) | |
app.get('/login/:name', (req, res) => { | |
// set a cookie for the person with the name | |
res.cookie('name', req.params.name) | |
res.send('Thanks for logging in, ' + req.params.name) | |
}) | |
// prefixed route | |
app.use('/dogs', dogRoutes) | |
// app.use((req, res) => { | |
// console.log(req.method) | |
// if (req.method === 'GET') { | |
// if (req.url === '/') { | |
// res.send('welcome to the home page') | |
// } else if (req.url === '/kanye') { | |
// res.send('YEEZY') | |
// } else { | |
// res.send('No page for this.') | |
// } | |
// } | |
// }) | |
app.listen(PORT, '0.0.0.0', () => { | |
console.log('AYYY WE REALLY OUT HERE LISTENING ON', PORT) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment