0

I am trying to make one router for login page post and get request.

below is my code :

  app.get('/login', getLoginPage);
  app.post('/login', doLogin);

Here I am using app.all to combine both , but I am not getting any idea how to handle it in all.

What I have tried so far .

app.all('/login', handler); 
var handler = function(req, res){
   console.log(req);
   if(req.method=="POST")
   {
   getLoginPage;
   }
   if(req.method=="GET")
   {
   doLogin
   }
}

const {getLoginPage,doLogin} = require('./controller/login');

Here app.get and app.post working fine for me except app.all. Here I am using express js. Please suggest .

Anuj Gupta
  • 65
  • 1
  • 8
sradha
  • 2,216
  • 1
  • 28
  • 48

5 Answers5

3

I would do it like so (full working example):

var express = require('express');
var app = express();


app.route('/test')
  .get((req, res) => getLoginPage(req, res))
  .post((req, res) => doLogin(req, res))

doLogin = (req, res) => {
  res.send('doLogin');
}

getLoginPage = (req, res) => {
  res.send('getLoginPage');
}

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});
David Joos
  • 926
  • 11
  • 16
  • 1
    i've added a full working example. If you can't make it run please check your node/express version. – David Joos Feb 01 '19 at 07:04
  • 1
    i have installed: node 10.14.1 and use express 4.16.3 - not sure but i would guess it should work with your versions. Isn't that example working for you? How do you test it and what do you expect? When this example is running, a simple get and post request to the route `localhost:3000/test` should reply with either 'doLogin' or 'getLoginPage' – David Joos Feb 01 '19 at 07:15
  • by mistake, I wrote localhost:4000/test instead of localhost:4000/login, Here my aim is to write in one code , not in get() and post() both its increasing number of lines . Its for learning purpose , I am a newbie to node js and learning it. Thank you foe the suggestion . – sradha Feb 01 '19 at 07:31
  • 1
    Always a good approach to decrease lines of code - but in this case you have to distinguish between get and post - either you do it with an `if else` statement on the `req.method` (see @ROAL answer) or you use the callbacks for this. I would recommend the callbacks as it is suggested by the express documentation. – David Joos Feb 01 '19 at 07:36
1

HOISTING!!!

It's really just scope. You have a function expression, not a declaration. Declarations get hoisted but definitions do NOT. So when you call handler, handler is not defined yet since it's function expression comes bellow it.

So no need to change your code, except the order of things. Do this and it will work

var handler = function(req, res) {
  if (req.method == "POST") {
    console.log("POST");
  }
  if (req.method == "GET") {
    console.log("GET");
  }
};

app.all("/login", handler);

Read about scopes in function declaration vs expression

einacio
  • 92
  • 9
0

Try following use method. It will accept all types of methods and you can apply conditions there:

app.use('/login', function (req, res, next) {
  console.log('Request Type:', req.method)
  if(req.method=="POST")
  {
    getLoginPage;
  }
  else if(req.method=="GET")
  {
    doLogin;
  }
});
Yuva
  • 131
  • 1
  • 6
0

You need to invoke the functions in your app.all handler.

app.all('/login', handler);
var handler = function (req, res) {
    console.log(req);
    if (req.method == "POST") {
        getLoginPage(req, res);
    }
    if (req.method == "GET") {
        doLogin(req, res);
    }
}
ROAL
  • 1,749
  • 1
  • 14
  • 21
  • after doing this I am getting this error ; Route.acl() requires a callback function but got a [object Undefined] – sradha Feb 01 '19 at 06:55
  • 1
    @sradha sorry, you're not using `next` in your handlers. I've edited the answer. @DavidJoos has a better suggestion anyways. See his answer. – ROAL Feb 01 '19 at 06:58
0

Tried invoking the functions and pass the req and res parameters to those functions

app.all('/login', handler); 
function handler(req, res){
  if(req.method=="POST"){
    getLoginPage(req, res);
  } else if(req.method=="GET"){
    doLogin(req, res);
  }
}

edit: change to function declaration instead of function expression to avoid hoisting issues

aldyadk
  • 41
  • 4