0

I'm trying to learn JavaScript and full-stack development and I can't quite wrap my head around how to use express with JavaScript properly ....

my question goes like this :

in every tutorial iv'e seen online the following syntax is being used :

app.get('/',function(request,response,next){

 var something = request.body.something;
 if(something){do stuff...}
 else{do other stuff...}

})

what if I wan't to not use anonymous functions but predefined named ones?

how would I use them in express methods?

will something like this be o.k? :

function doStuffwithTheRequest(request,response,next){

 var something = request.body.something;
 if(something){do stuff...}
 else{do other stuff...}

};


app.get('/',doStuffwithTheRequest(req,res,next));

and if so what would be the proper way to pass the parameters in this manner ?

Iv'e tried coding like this but i cant seem to be able to pass the parameters to the function when it's predefined ...


I got an answer for the question above but I would like to go over an add-on to the question ...


How would I go about taking out the the inner callback function from something like this:

function doStuffwithTheRequest(req,res,next){

 Somemiddleware.someMethod({parameter:req.session.value},function(err,returningparameter){

 res.locals.info = returningparameter;
 if(something){do stuff...}
 else{do other stuff...}

 })
};

app.get('/',doStuffwithTheRequest);

once I take it out an error is being thrown :

res is not defined

Gal Ratzkin
  • 124
  • 2
  • 8

1 Answers1

1

The way you are doing it, you are calling the named function with no arguments. You should just pass the function itself:

app.get('/',doStuffwithTheRequest);

Just make sure the function definition has the correct arguments (req, res, next), which it does in your example.

Steve
  • 132
  • 1
  • 5
  • 1
    Because app.get() expects only a function definition. If you are calling it as "doStuffwithTheRequest(req,res,next)", you are immediately executing that function and passing the result (nothing) back to app.get(). – Steve May 22 '17 at 19:22
  • Sorry, the "doStuffwithTheRequest(req,res,nex)" was an example. I meant that if you add the parentheses to "doStuffwithTheRequest", you are immediately calling the function there, as opposed to passing the function definition in to app.get(). – Steve May 22 '17 at 19:26
  • sorry if i'm being slow but how is that different from an anonymous function ? isn't it being executed immediately ? – Gal Ratzkin May 22 '17 at 19:30
  • No. Adding the parentheses and arguments the way you were trying to do it would be equivalent to: app.get('/', function(request,response,next){...}()). Notice the extra set of parentheses on the end. Doing this would execute the anonymous function and send its return value to app.get(). By leaving the last set of parentheses off, you are passing the function itself to app.get(). This works the same whether it is an anonymous or named function. – Steve May 22 '17 at 19:35
  • Thank you !!! I guess i have to dig some more into js functions and function calls ... – Gal Ratzkin May 22 '17 at 19:44