ExpressJs Routing Fundamentals
ExpressJS is a framework for building web applications using Node.js. One of the key features of ExpressJS is its ability to handle routing. Routing is the process of determining which function to call for a given URL. ExpressJS provides a simple and flexible way to handle routing.
Syntax
ExpressJS provides a set of methods for defining routes. The basic syntax for defining a route is as follows:
app.METHOD(PATH, HANDLER)
METHOD
is the HTTP method (get, post, put, delete, etc.)PATH
is the URL pathHANDLER
is the function to be called when the route is requested
Example
Let's create a simple ExpressJS application with two routes.
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/about', (req, res) => {
res.send('About Page')
})
app.listen(3000, () => {
console.log('Server started at http://localhost:3000/')
})
In the above code, we have defined two routes. The first route responds to requests to the root URL and the second route responds to requests to the /about
URL. Both routes use the get
method.
Output
Let's run our application and see the output.
Server started at http://localhost:3000/
Now if we navigate to http://localhost:3000/
in a web browser, we should see the message "Hello World!". If we navigate to http://localhost:3000/about
, we should see the message "About Page".
Explanation
ExpressJS provides a number of methods for defining routes, including get
, post
, put
, delete
, all
, and more. The get
method is used to define a route to respond to GET requests. The post
, put
, and delete
methods are used to define routes to respond to POST, PUT, and DELETE requests, respectively. The all
method is used to define a route to respond to all HTTP methods.
The PATH
parameter can be a string, a regular expression, or an array of strings and regular expressions. When a request matches a route, the HANDLER
function is called with two arguments: req
(the request object) and res
(the response object).
Use
Routing is a fundamental part of building web applications. ExpressJS provides a simple and flexible way to handle routing. You can define routes for different HTTP methods and you can use regular expressions to match complex URL patterns. You can also use middleware functions to process requests before they reach the route handler.
Important Points
- Routing is the process of determining which function to call for a given URL.
- ExpressJS provides a simple and flexible way to handle routing.
- The basic syntax for defining a route is
app.METHOD(PATH, HANDLER)
. - The
PATH
parameter can be a string, a regular expression, or an array of strings and regular expressions. - The
HANDLER
function is called with two arguments:req
andres
. - You can use middleware functions to process requests before they reach the route handler.
Summary
In this article, we have learned about ExpressJS routing fundamentals. We have looked at the syntax for defining routes, and we have seen an example of a simple ExpressJS application with two routes. We have also discussed some important points to keep in mind when working with ExpressJS routing.