How to have the server read query strings then send that page a confirmation

I want the server in my nodejs to read query strings from a page which it can with this code app.route("/register").post(async (req, res) => { app.get("/redirect", function (req, res) { const username = req.query.username const email =; const password = req.query.password; });
But now I want the server to tell /redirect to either let them login or not but I can only use express in the server file and not in the js file of /redirect. How do I make express send a confirmation from the server to /redirect to let them login.

explain that bit, you’re registering a new route inside the handler of another route? because you need the /redirect route to do different things based on what a user sent to the /register route?

usually these websites are built to allow different users to access it at the same time. so they have to set a cookie or equivalent to tell which user is requesting either /register or /redirect. and then there’s, for example, a database that stores various per-user state. but does that really fit here? you later talk about letting the user log in, so it also sounds like at that point you don’t yet know what user they are.

I want to have the user login or register which sends them to /redirect with the query strings. Then the server.js gets that query string and it will know whether they came from /login or /register due to /register having more parameters. It then checks the data by either putting it in the database or checking it and then telling /redirect to redirect to the login page or the home page.

I mainly just want to know how to have the server tell /redirect that they are good to login

Why would this matter if I have a login, Do you mean if 2 people are requesting to login, the server won’t know who requested it?

trying to understand this. can you be more precise about the following:

  • “have the user login or register” – meaning what, posting their credentials to the server as a form submission? or even more?
  • “sends them to /redirect” – “send” in particular, do you have an idea of how to do this? e.g. server side redirect?

or did you mean that you want the browser to post the credentials directly to /redirect within the query string instead of the request body, and where /login and /redirect only render the forms?

When I say “have the user login or register.” I’m talking about a page that has a form submission to login. When I say “sends them to /redirect” I’m talking about when you press submit on the form it uses GET to redirect them to /redirect.

For the last part, I’d rather have the credentials inside of the request body (For security purposes because I don’t see many sites doing this) but I’m using query strings instead for simplicity. As of right now /login and /register only render the form and /redirect takes the query strings and shows them to the server.

thanks for the clarification. you’ll need to make some changes:

  1. /login and /register routes should handle get, not post. it’ll be only the browser opening it as a webpage.
  2. in the pages that those two routes serve, set the form action attribute to /redirect.
  3. to have the credentials sent in the request body, set the form method attribute to post.
  4. change the /redirect route to handle post, not get, now that you’ll be having the form submission be sent as a POST request.
  5. move the route registration of"/redirect", ...) out to be next to the other routes, so that it runs right away. it’s just the normal way to do it. it won’t make the handler run immediately, it’ll still only run when users submit the form.
  6. set up a middleware Express 4.x - API Reference to read the request body stream into a req.body object.
  7. change the inside of that /redirect handler to use req.body instead of req.params now that you’ll be having the credentials sent in the request body.
1 Like

When you talk about step one when you say the routes should handle get what do you mean by handle because the code serving on the login and redirect pages is

<h1>Login Page</h1> <form action="/redirect" method="get"> <label for="email">Email:</label> <input type="email" id="email" name="email" /> <br /> <label for="password">Password:</label> <input type="password" id="password" name="password" /> <br /> <input type="submit" value="Submit" />
And if I were to make the method='post' like in step 3 then it would say “Cannot POST /redirect”

that’s definitely part of it. but that’s the HTML code that runs on the browser. there’s actually another piece of code that runs on the server when the browser requests that page. that’s the code that I’m referring to as what “handles” the request to that route. and that piece of code tells the server to send the HTML code to the browser as the response.

good work so far on steps 2 and 3. the “Cannot POST /redirect” will be resolved when you get to step 4.

1 Like

Makes sense but how exactly would I do something like move the route registration of"/redirect", ...) like in 5 or change the /redirect route to handle post like in 4

for 4: see that “get” in the app.get("/redirect", ...); statement? that tells express to look for HTTP GET method requests to that /redirect path. but we want to have it look for HTTP POST method requests. otherwise if users submit your form and send a POST request, they’ll get a “method not allowed” error.

you’re already registering a POST handler for a different path elsewhere, so I’ll give you some time to understand the code you currently have. but post again if you need a hint.

for 5: I mean moving that piece of code out of the handler for /register. like cut and paste. and fix indentation if your code isn’t actually crammed all into one line haha.


I think I have it down but i’m going to go down the list to check with you

  1. app.get("/login", function (request, response) { response.sendFile(__dirname + "/public/trolled/login/login.html"); }); app.get("/register", function (request, response) { response.sendFile(__dirname + "/public/trolled/register/register.html"); });
  2. I have my code in login.html and register.html as so
    <form action="/redirect" method="post">
  3. (^This is for 3 as well)
  4."/redirect", function (request, response) { response.sendFile(__dirname + "/public/trolled/redirect/redirect.html"); });
  5. I put it at the top here but i’m not sure if its correct
  6. My login and register code in the server looks like this `app.route(“/login”).post(async (req, res) => { const { email, password } = req.body;

app.route(“/register”).post(async (req, res) => {
const { username, email, password } = req.body;`
7. (^This is for 7 as well)

hey, this is shaping up!

what’s in redirect.html?

rather than serving a static html page from /redirect, that’s where you should put your code to handle logins and registrations (at least according to you initial designs).

Thats kinda what I was trying to do at first but how do I have stuff like express and Sqlite work outside of server.js

oh I didn’t mean to say you should put that code in a webpage. put that code still in server.js. in the function that handles post requests to /redirect.

You mean in"/redirect", function (
And can I get an example of what to put there?

use the code that you were about to put in the post /login and post /register handlers

Where exactly should I put the code in the handlers though