Unopinionated minimalist web framework for NodeJS.
Hello world
HTTP Methods - GET, POST, PUT, DELETE, PATCH
There are many more methods as options, merge, purge, search, etc.
Dynamic Routing
Dynamic routing means, when the path can be vary, dynamic routes gives a way to get the value of the dynamic part.
For example route /user/443 the part user-id 443 is dynamic and depends on the user’s id, so we cant create hardcoded paths for all possible ids.
/user/:userId→ here :userId becomes the dynamic parameter in the URL and the value can be accessed.
Multiple dynamic parameters:
Optional Parameters:
Route Order Matters Express evaluates routes in the order they are defined, so more specific routes should be defined before dynamic routes to avoid potential conflicts. In the below example, if the dynamic route is places above the /users/all, then it will be never matched with /users/all
Query Parameters
Query Params are data that is sent as key value pair after the ? in the URL, and multiple values are separated by &
These values can be accessed as below:
ExpressJs Router
A router object is an instance of middleware and routes. It is capable of performing middleware and routing functions.
A router can be mounted to express as a middleware function.
Benefits
A router helps is modularizing the code. Otherwise the complete route paths would be needed to define in the same server.js file.