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: -
router.get('/users/firstSignIn', function(req, res, next) {
-
res.render('uesrs/firstSignIn', { title: 'title' });
-
});
-
And this signup function in my users controller: -
exports.create = function(req, res, password) {
-
-
if(req.method.toLowerCase() != "post") {
-
res.render("signup.jade", {layout: false});
-
}
-
else {
-
new user(req.body).save();
-
res.redirect("/users/firstSignIn");
-
//res.send("ok"); // USE THIS TO REDIRECT AFTER SIGNUP
-
}
-
}
-
This is in my app.js: -
app.get('/users/create', users.create);
-
app.post('/users/create', users.create);
-
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?
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: -
exports.create = {
-
get: function(req, res, next) {},
-
post: function(req, res, next) {}
-
};
-
-
app.get('/users/create', users.create.get);
-
app.post('/users/create', users.create.post);
-
4 1764
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: -
exports.create = {
-
get: function(req, res, next) {},
-
post: function(req, res, next) {}
-
};
-
-
app.get('/users/create', users.create.get);
-
app.post('/users/create', users.create.post);
-
I'm not sure if it's just my system (my work computer is pretty ordinary), but I changed it to this: -
exports.create = {
-
-
post: function(req, res, next) { // password
-
-
if(req.method.toLowerCase() != "post") {
-
res.render("signup.jade", {layout: false});
-
}
-
-
else {
-
new user(req.body).save();
-
}
-
},
-
-
get: function(req, res, next){
-
res.redirect("/users/firstSignIn");
-
}
-
-
}
-
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.
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.
That was correct. The function now looks like this: -
-
exports.create = {
-
post: function(req, res, next) {
-
new user(req.body).save();
-
res.redirect("/users/firstSignIn");
-
},
-
get: function(req, res, next){
-
res.redirect("/users/firstSignIn");
-
}
-
}
-
-
Now I just need to customise it.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: sampriti |
last post by:
Hi,
Is it possible to redirect user to same webserver until
his session ends?
Thanks in advance
|
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...
|
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...
|
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...
|
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...
|
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:...
|
by: Jon Paal |
last post by:
what is property to redirect user after successful login ?
<asp:Login ID="Login1" runat="server" ?????
|
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...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |