Last active
April 16, 2020 19:50
-
-
Save ericmacfa/95734ff2a312247ac718f22623a1b7fe to your computer and use it in GitHub Desktop.
Debugging for https://www.reddit.com/r/node/comments/g1jkpz/getting_router_nodejs_error_beginner_please_help
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 app = express(); | |
const router = express.Router(); | |
const makeMockHandler = (name) => (req, res) => { | |
const message = `${name} --> ${req.originalUrl}`; | |
console.log(message); | |
res.json({ handler: name }); | |
}; | |
// Endpoint handlers, mock handlers since the issue is with route handler resolution | |
router.post('/', makeMockHandler('POST /')); | |
router.get('/', makeMockHandler('GET /')); | |
router.get('/:id', makeMockHandler('GET /:id')); | |
router.patch('/', makeMockHandler('PATCH /')); | |
router.delete('/', makeMockHandler('DELETE /')); | |
router.post('/login', makeMockHandler('POST /login')); | |
// Nest the endpoint handlers under a shared `users` api route | |
app.use('/api/users', router); | |
app.listen(80); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment