0

I quite don't understand the difference between these two:

app.get('*', function(req, res, next) {
  next();
 //ROUTE 1
});

app.get('*', function(req, res) {
  res.redirect('/some');
  //ROUTE 2
});

app.get('/some', function(req, res) {
  res.send("success");
  //ROUTE 3
});

When I try making request to ROUTE 1, I get response success but ROUTE 2 doesn't show this response. Why is that?

What I want to do is:

Every request should pass from ROUTE 1 and the control should be handled to a SPECIFIC route, which I would write in it ROUTE if-else statement (not like next(), which sends control to next MATCHING route).

For example:

app.get('*', function(req, res, next) {
  if(x==y){
    //call SPECIFIC route 3 
  } else {
    // call SPECIFIC route 4 (another route)
    //ROUTE 1
});

I tried to do it with redirect but it's not working.

Thank you.

EDIT:

  • Routes would be: /checkIfSession exists. I would use express-session to check if user's username exists or not in session.

If exists, I want to send control to if otherwise else.

Assume the requests are:

  1. http://198.168.43.200:3000/checkIfSession

  2. http://198.168.43.200:3000/some

(I will call only 1st request).

EDIT 2: I tried following but I don't get any response when I request:

app.use(function (req, res, next) {
    if(2==2){
        res.redirect("/session");
    } else {
        res.end("else");
   }
});

app.get("/session", function(req, res){
    res.write("session");
    res.end();
});

app.get("/some", function(req, res){
    res.write("some");
    res.end();
});

Request: /some

Vikas
  • 720
  • 1
  • 9
  • 30
  • 1
    What **exact** uris are you using to try this? – Wiktor Zychla Jun 21 '17 at 11:29
  • 2
    what is x and y in your example some variable in your server side or something you are getting from the client side ? and I you want all your routes to go through a particular condition you can use a `middleware` – warl0ck Jun 21 '17 at 11:38
  • `res.redirect` will work for your requirement. No need of next. But If you mention * ,any request will go into this route , regardless of your definition of that route and hence you may end up in an infinite redirection in your case. eg : if you have defined /test after * then,request to /test will be given to *. – vinoth h Jun 21 '17 at 11:40
  • Please check the edits. – Vikas Jun 21 '17 at 11:41
  • @vinothh I will handle the infinite thing. 2ndly, redirect is done on server side or client side? – Vikas Jun 21 '17 at 11:42
  • 2
    Looking at your edits I agree with @warl0ck you want to implement some form of `middleware`. – George Jun 21 '17 at 11:42
  • Okay, I would use a middleware like `app.use(function())`, but still inside that middleware, I would need to check for if and else and redirect to a specific route. What should I use? – Vikas Jun 21 '17 at 11:44
  • So now just to be sure you want, all your requests to pass through some king of check i.e. if the session is valid or not ?? – warl0ck Jun 21 '17 at 11:44
  • @warl0ck exactly. Then I would use control statement to "redirect" to some another route(s) – Vikas Jun 21 '17 at 11:45
  • @VikasKumar instead of this you can pass the function as well before sending any details of that protected route. – warl0ck Jun 21 '17 at 11:45
  • Could you please explain? – Vikas Jun 21 '17 at 11:46
  • @VikasKumar Redirection is done by the server. Use express session to store the user-name. If user exists redirect him to your page. But put a check in that route too, that whether the user exists. – vinoth h Jun 21 '17 at 11:48
  • @warl0ck your last comment? I quite didn't understand. Also, can I use redirect as told by @vinoth? – Vikas Jun 21 '17 at 11:51
  • please check the answer – warl0ck Jun 21 '17 at 11:52

1 Answers1

2

I suppose if you want your routes to go through some kind of authentication first you can use middleware in your routes.

Below is sample code:

app.get('/some', checkSession, function(req, res) {
  res.send("success");
});

// this is the middleware function

function checkSession(req, res, next) {
    // do your checking here
    if (x===y) {
        next();
        //continue users access to protected API
    } 
    else {
        res.redirect('/');
        // redirect user to the default login page
    }
} 

In this above example there are 2 Cases

Case1:

x === y as from your given example I'am assuming users is logged in, so when the user is accessing /some section of your website he will receive Success from the server.

This is the use of your next() function i.e. it continues the execution of that api or sends the data whatever the user is requesting. Something similar to continue in your programming.

Case2:

x!==y now this will be the case where user is not authenticated or logged in and user is still trying to access the /some section of your website. In this case user will be redirected to login page of whatever you have designed for your website for him/her to re-enter his/her credentials.

Here your redirect function redirects the user without sending any data. Something similar to the break.

warl0ck
  • 3,356
  • 4
  • 27
  • 57
  • This finally suggests that I should use res.redirect to "call" a specific route? This was my fundamental query. – Vikas Jun 21 '17 at 11:53
  • see here next() is continuing the access of user to the api, whereas redirect is sending the user to that page. I'll update the answer to make it more clear – warl0ck Jun 21 '17 at 11:55
  • Well now I'm getting this error: XMLHttpRequest cannot load http://192.168.43.200:3000/some. Response for preflight is invalid (redirect) – Vikas Jun 21 '17 at 12:05
  • I am making this request from angularjs's ajax – Vikas Jun 21 '17 at 12:06
  • atleast is now your routes are working fine ?? c=you can check them by using `console.log` to make sure and as for the error you are describing see this: https://stackoverflow.com/questions/33660712/angularjs-post-fails-response-for-preflight-has-invalid-http-status-code-404 – warl0ck Jun 21 '17 at 12:17
  • I'll respond you soon. – Vikas Jun 21 '17 at 12:32
  • sure take your time :) – warl0ck Jun 21 '17 at 12:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147282/discussion-between-vikas-kumar-and-warl0ck). – Vikas Jun 21 '17 at 14:22
  • can you answer this? https://stackoverflow.com/questions/44712538/cant-save-express-session-in-nodejs – Vikas Jun 23 '17 at 04:21
  • 1
    sure, but first is this question of your answered ?? – warl0ck Jun 23 '17 at 04:55