Express Routing Flashcards
What is routing in Express.js?
Routing directs requests to the corresponding controllers/methods based on the request’s method and url (path).
Difference between middleware and routing
Middleware are functions and routing is a process.
Middleware can perform some actions, end the request-response cycle or call the next middleware function.
How to implement routing? How do you define routes in Express.js?
app.get(“/”,(req,res)=>{ res.send(“Something”) })
What are Route Parameters?
A route/path might have dynamic part. That’s the Route Parameter.
app.get(‘/users/:userId’)
req.params.userId is a route parameter.
What are router object & router methods and how to implement them?
const router = express.Router();
router.get(…)
It’s the same as using app.use() but this improves modularity.
To mount the router: app.use(“/api”, router)
What are router methods?
router.get(path,callback)
router.post(path,callback)
router.put(path,callback)
router.delete(path,callback)