Skip to content

Commit

Permalink
Refactors routers to include path
Browse files Browse the repository at this point in the history
Router.use() is based on path, not router instance.
This means routerA can `.use` a middleware and end up effecting routerB.
The user router (and all others) was running through
the requireToken middleware as a result.

This is a documented issue with express router:
pillarjs/router#38
  • Loading branch information
esalling23 committed Aug 24, 2021
1 parent c5bfc25 commit 472ec98
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/routes/order_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ const deleteOrder = (req, res, next) => {
router.use(requireToken)

// Create & Index routes
router.route('/orders')
router.route('/')
.post(createOrder)
.get(indexOrders)

// Update, Delete, & Show routes
router.route('/orders/:id')
router.route('/:id')
.patch(findOrderMiddleware, updateOrder)
.get(findOrderMiddleware, showOrder)
.delete(findOrderMiddleware, deleteOrder)
Expand Down
4 changes: 2 additions & 2 deletions app/routes/product_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const Product = require('../models/product')
const { handle404 } = require('../../lib/custom_errors')

// INDEX
router.get('/products', (req, res, next) => {
router.get('/', (req, res, next) => {
Product.find()
.then(products => res.status(200).json({ products }))
.catch(next)
})

// SHOW
router.get('/products/:id', (req, res, next) => {
router.get('/:id', (req, res, next) => {
Product.findById(req.params.id)
.then(handle404)
.then(product => res.status(200).json({ product }))
Expand Down
6 changes: 3 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ app.use(express.urlencoded({ extended: true }))
app.use(requestLogger)

// register route files
app.use(productRoutes)
app.use(orderRoutes)
app.use(userRoutes)
app.use('/products', productRoutes)
app.use('/orders', orderRoutes)
app.use('/auth', userRoutes)

// register error handling middleware
// note that this comes after the route middlewares, because it needs to be
Expand Down

0 comments on commit 472ec98

Please sign in to comment.