Attempting to create a redirection landing page

I have a process where I create a new user and save it in my database with the following code snippet:

const newUser = new User({
    username: userId,
    password: pass,
    nameOfUser: user_name,
    emailOfUser: user_email
);
newUser.save();

res.redirect("/redirectpage");
setTimeout(function(){
    res.redirect("/");
},5000);

Upon saving the user, I want to implement a countdown redirect page. It is visually represented like this:

https://i.stack.imgur.com/wc94c.png

Once the 5-second counter reaches 0, the user should be redirected back to the login page, which is the home route. I used setTimeout for this functionality. However, my application crashes and shows errors if I do not comment out res.redirect("/redirectpage").

https://i.stack.imgur.com/zkZfq.png

Answer №1

This particular snippet of code attempts to redirect the user twice within the same request - first to /redirectpage and then again to /. However, this is not possible due to the fact that calling res.redirect() for the first time will effectively complete the request, making it impossible to perform a second redirect. To resolve this issue, you should consider moving the redirect logic to the client-side javascript. One potential solution could look like this:

setTimeout(function() {
    window.location.replace("[full redirect url]");
}, 5000);

If you want to delve deeper into the topic of redirecting from the client-side, feel free to check out this informative answer.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing AngularJS table filters on user click

As a newcomer to angularjs, I am attempting to implement a filter on click. The user will select a source and destination, then click on the filter button. The table should display results based on the input. Upon page load, the table should already contai ...

Trouble with minified scripts on a particular server

Apologies if this has already been addressed, but I've been searching for a solution for hours. I created a basic website that works perfectly on my own hosting. However, when I transfer it to new hosting (same company, same package, different server ...

Incorporating various language URLs in Next.js using next-i18n-next functionality

Thinking about using the next-i18next internationalization library. Check out next-i18next on GitHub Curious about how to change the language in the path of my URL. For example: /about-us /over-ons -> takes you to the Dutch version of the about us p ...

Looking for regex to extract dynamic category items in node.js

Working on node.js with regex, I have accomplished the following tasks: Category 1.2 Category 1.3 and 1.4 Category 1.3 to 1.4 CATEGORY 1.3 The current regex is ((cat|Cat|CAT)(?:s\.|s|S|egory|EGORY|\.)?)( |\s)?((\w+)?([. ...

Is there a way to verify DNSSEC compatibility using node.js code?

Struggling with incorporating DNSSEC compliance checks into my web analytics tools using NodeJS. The standard library's dns module does not seem to accept DNSSEC record types such as rrsig, ds, nsec, and nsec3. Despite checking the documentation, thes ...

Clicking to reveal a v-date-picker v-menu and automatically focusing on a v-text-field within it?

I implemented a date-picker component in my app following the instructions from Vuetify. To enhance usability for desktop users, I removed the readonly attribute to allow manual input. Now, desktop users can easily navigate through form fields using th ...

Display Nvd3 Pie Chart percentages in decimal format

I have integrated Nvd3 into my Angular project to create various types of charts. Utilizing the angular directive from Krispo's website, I am currently working on a pie chart that displays values in percentages. However, the displayed values are round ...

Creating interactive web pages for scheduling tasks

I'm struggling with how to implement this concept. Imagine I have a School website that features a schedule page for 30 upcoming courses. When a course is clicked, it should lead to a new page displaying specific information about that course (such a ...

What could be the reason behind ng-bind-html only displaying text and not the link?

Utilizing ng-repeat to exhibit a list on my webpage. One of the fields in my data contains a URL that I want to display as an actual link within my HTML page. Please refer to the screenshots below: My HTML: My rendered page: I have included the angular- ...

JQuery fails to retrieve accurate width measurements

Utilizing this code snippet, I have been able to obtain the width of an element and then set it as its height: $(document).ready(function(){ $(".equal-height").each(function(){ var itemSize = $(this).outerWidth(); cons ...

Setting up a Node.js WebSocket server on a shared cPanel hosting environment

Recently, I obtained a shared hosting plan with cPanel support for nodejs applications. Through the "Setup Node.js App" feature, I am able to define and set up a node.js app on my server. My goal is to create a websocket, and fortunately, they have opened ...

Tips for incorporating material-ui icons into the column component of a data-grid component

I am currently working with the material-ui data-grid and I would like to display Edit icons from material-ui next to each row. Unfortunately, the data-grid does not provide any props specifically for this purpose. Below is the code snippet: import React ...

Tips for automatically inserting a "read more" link once text exceeds a certain character count

Currently utilizing an open-source code to fetch Google reviews, but facing an issue with long reviews. They are messing up the layout of my site. I need to limit the characters displayed for each review and provide an option for users to read the full rev ...

Launch various tasks concurrently with npm scripts by utilizing PM2

I need to initiate multiple processes using pm2. I know how to start one process for npm run start: pm2 start npm -- start However, when I attempt to do something similar like pm2 start npm -- event for npm run event, it doesn't create a new proces ...

Warning: Attempting to destructure the property 'name' from 'req.body', which is undefined, is causing a TypeError

Currently, I am diving into the world of MERN Stack web development and running into a unique issue. When using Postmate to input data from the body to the database, everything works smoothly when done from the server.js file. However, when attempting the ...

Investigating Jquery Flip Card Issues

Looking to create a set of flip cards using HTML, CSS, and jQuery. Currently facing an issue where only the first card is flipping when clicked. Any suggestions on how to modify the jQuery code to make it work for all cards would be highly appreciated. C ...

Should JSON APIs be expected to return strings or JavaScript Objects?

Suppose I request subscriber data from Mailchimp, and they respond with an http request containing JSON in the body. Should I be able to access it like this: var thingy = body.property; Or do I need to do it this way: var object = JSON.parse(body); var ...

Display or conceal a <div> segment based on the drop down selection made

A dropdown menu controls the visibility of certain div elements based on the selection made. While this functionality is working for one dropdown, it's not working for another even though the code is very similar. I've tried various solutions but ...

Challenges faced with dividing a string in JavaScript

Having trouble splitting a string in JavaScript, I can't seem to make it work. This is the JavaScript code I am using: var str = 'TestApplication20 Application200'; var parts = str.match(/(\d+)(\D.+)/).slice(1); var id = parts[0 ...

Add empty objects to an array using a push function following the Vue JS constructor function

During my learning journey with Vue JS, I encountered an issue while attempting to populate an array with objects using a constructor function and the push method. Surprisingly, in Vue JS, the push function inserts a blank object into the array instead of ...