Working with Express JS
Mounting Routes In Express JS
We noticed that when we use a middleware, it is applied on all the requests. But there is an overloaded version of the use()
method that allows us to apply the middleware on specified routes.
To create a unique router in Express we use the express.Router()
method. Lets create a unique router for our movies resource. Instead of using the app object as the router lets use our unique movie router.๐๐ผ
We can create different routers for our dynamic resources. The moviesRouter()
is a middleware and it other to implement it we use the use()
function but in other to apply the router to our dynamic resource, inside the use()
function we pass the endpoint as the first parameter and then the router. Lets refactor the code base๐๐ผ
And our server is still working perfectly, we still get our response๐๐ผ
So in summary, we created a new router and we are using it for the movies route and we are mounting the router to our movies endpoint using the use()
method.
That's all for this lesson.
Next: Creating and using Routes Module
See you soon๐๐ซกโ๐ผ...