Middlewares-and-Routers Flashcards
Question
Answers
How do you use middleware?
app.use(middlewareName())
How do you create middleware?
const middlewareName = (req, res, next) => { next()}
How do you create a router?
const router = express.Router()
How do you add HTTP methods to your router?
router.route(‘subURL’).method(controller).method(controller)
How do you use a router?
app.use(‘URL’,router)
What are param middlewares?
They are middlewares that run for a URL, only if a specific parameter exists
How do you create a param middleware?
const middleware = (req, res, next, val) => {//val is the param value next()}
How do you use the param middleware?
By assigning it to a router e.g. router.param(‘url’, middleware)
How do you chain middlewares for a single route?
Router.method(middleware, middleware, middleware)