Skip to content

Instantly share code, notes, and snippets.

@rebz
Created October 17, 2020 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rebz/5e5a16c8f7811ca42751d28afa6808f8 to your computer and use it in GitHub Desktop.
Save rebz/5e5a16c8f7811ca42751d28afa6808f8 to your computer and use it in GitHub Desktop.
Example of progressive abstraction for routes in a Node Express application.
// OPTION 1
router.get('/user', function(req, res) {
res.send('yay')
})
// OPTION 2
function getUser(req, res) {
res.send('yay')
}
router.get('/user', getUser)
// OPTION 3
const getUser = function(req, res) {
res.send('yay')
}
router.get('/user', getUser)
// OPTION 4
const UserController = {
getUser: function(req, res) {
res.send('yay')
}
}
router.get('/user', UserController.getUser)
// OPTION 5
// /controllers/UserController.js
module.exports = {
login: function(req, res) {
res.send('yay')
},
register: function(req, res) {
}
}
// routes/index.js
const UserController = require('./../controllers/UserController.js')
router.get('/user', UserController.getUser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment