473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Express.js redirect user after sign up

97 64KB
I can't find an answer to this: What do I do after a new user signs up to my express.js app?

I have a view, and this in the routes/index file that correspnds:
Expand|Select|Wrap|Line Numbers
  1. router.get('/users/firstSignIn', function(req, res, next) {
  2.   res.render('uesrs/firstSignIn', { title: 'title' });
  3. });
  4.  

And this signup function in my users controller:
Expand|Select|Wrap|Line Numbers
  1. exports.create = function(req, res, password) {
  2.  
  3.   if(req.method.toLowerCase() != "post") {
  4.     res.render("signup.jade", {layout: false});
  5.   }
  6.   else {
  7.      new user(req.body).save();
  8.      res.redirect("/users/firstSignIn");
  9.      //res.send("ok");  // USE THIS TO REDIRECT AFTER SIGNUP
  10.   }
  11. }
  12.  

This is in my app.js:
Expand|Select|Wrap|Line Numbers
  1. app.get('/users/create', users.create);
  2. app.post('/users/create', users.create);
  3.  

When I sign up a new user, I can't figure out how to redirect them. Am I going about this all wrong, or I just need to add to my controller?
May 12 '16 #1

✓ answered by Dormilich

you use two different functions for the get and post route, not one function that does it all.

if you want to use export, then you need to export two functions, not one:

Expand|Select|Wrap|Line Numbers
  1. exports.create = {
  2.     get: function(req, res, next) {},
  3.     post: function(req, res, next) {}
  4. };
  5.  
Expand|Select|Wrap|Line Numbers
  1. app.get('/users/create', users.create.get);
  2. app.post('/users/create', users.create.post);
  3.  

4 1764
Dormilich
8,658 Expert Mod 8TB
you use two different functions for the get and post route, not one function that does it all.

if you want to use export, then you need to export two functions, not one:

Expand|Select|Wrap|Line Numbers
  1. exports.create = {
  2.     get: function(req, res, next) {},
  3.     post: function(req, res, next) {}
  4. };
  5.  
Expand|Select|Wrap|Line Numbers
  1. app.get('/users/create', users.create.get);
  2. app.post('/users/create', users.create.post);
  3.  
May 12 '16 #2
tdrsam
97 64KB
I'm not sure if it's just my system (my work computer is pretty ordinary), but I changed it to this:

Expand|Select|Wrap|Line Numbers
  1. exports.create = {
  2.  
  3.     post: function(req, res, next) { // password
  4.  
  5.         if(req.method.toLowerCase() != "post") {
  6.         res.render("signup.jade", {layout: false});
  7.         }
  8.  
  9.         else {
  10.             new user(req.body).save();
  11.         }
  12.     },
  13.  
  14.     get: function(req, res, next){
  15.         res.redirect("/users/firstSignIn");
  16.     }
  17.  
  18. }
  19.  
And it's doing some strange things. It's posting the new user to the Db, but it's adding the user twice. It's also not loading the new page (/users/firstSignIn). The browser crashes with an error message saying: "the localhost page isn't working. localhost didn't send any data"

But when I check the db the data is there, as I said twice.
May 13 '16 #3
Dormilich
8,658 Expert Mod 8TB
first, if(req.method.toLowerCase() != "post") is pointless as the request method of a POST route is always POST. second, you never send a response after inserting data into the db.
May 13 '16 #4
tdrsam
97 64KB
That was correct. The function now looks like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. exports.create = {
  3.   post: function(req, res, next) {
  4.     new user(req.body).save();
  5.     res.redirect("/users/firstSignIn");
  6.   },
  7.   get: function(req, res, next){
  8.     res.redirect("/users/firstSignIn");
  9.   }
  10. }
  11.  
  12.  
Now I just need to customise it.
May 18 '16 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Noozer | last post by:
Is it possible to redirect a user to another web page using ASP code located in page body? I have some ASP code that runs after generating some output to the browser. It checks for a condition...
0
by: sampriti | last post by:
Hi, Is it possible to redirect user to same webserver until his session ends? Thanks in advance
1
by: New ASP.NET User | last post by:
I want user to redirect to another page after some interval. I used Httpresponse Page object (Response.redirect("default.aspx") but it transfers right a way... Any itdeas... Thanks in...
9
by: Shaun | last post by:
Anyone, I am trying to programmatically change user accounts in Windows NT 4. I am using Microsoft Visual Basic .NET 2003. The two things I am trying to do are as follows. 1. Set the User...
2
by: John | last post by:
On my web app I require a user to log in so I can validate them. At times I'll be sending emails out for a reporting page in the web app, How can I have the user goto the link I sent to them after...
3
by: j-in-uk | last post by:
Hi, I'm using the login control for asp.net 2.0. Once a user logs in I want to set timeouts for certain pages not the whole app, so if the page they on has expired I want to redirect the user...
1
by: vunet.us | last post by:
Imagine: user clicks the link which brings him/her to a secure page (let's call it page "X") which requires login session. If user is not logged in, he/she is redirected to a login page. Question:...
1
by: Jon Paal | last post by:
what is property to redirect user after successful login ? <asp:Login ID="Login1" runat="server" ?????
2
by: sindhudixit | last post by:
Hey, I am having a user fill out a form then the fields are going to uploaded to my database. So, at this point, when the user hits the submit button I want three things to happen: 1. The...
3
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I have a asp.net app. When session is invalid, how to redirect user to the login page? I don't want to add the code to redirect user to the login page into every page. Thanks, -Billy
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.