expressjsHow can I use Express.js to create a redirect?
Express.js is a web application framework for Node.js that allows you to create a web server and route requests. You can use Express.js to create a redirect by using the res.redirect()
function. This function takes two parameters, the first is the destination URL and the second is an optional status code.
Example code
app.get('/', (req, res) => {
res.redirect('/home')
})
This code creates a redirect from the root route /
to /home
.
The code consists of:
app.get('/', (req, res) => {
- This is the route handler for the root route/
.res.redirect('/home')
- This is theres.redirect()
function which takes the destination URL as a parameter and creates a redirect.
Helpful links
More of Expressjs
- How do I set up a YAML configuration file for a Node.js Express application?
- How do I find Express.js tutorials on YouTube?
- How can I use the x-forwarded-for header in Express.js?
- How can I create and use models in Express.js?
- How do I implement CSRF protection in an Express.js application?
- How can I set up X-Frame-Options in ExpressJS?
- How can I disable the X-Powered-By header in Express.js?
- How do Express.js and Spring Boot compare in terms of features and performance?
- How can I render HTML pages using Express.js?
- How can I use an ExpressJS webhook to receive data from an external source?
See more codes...