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

3
  • 2
    Just change the last line to app.get('/',doStuffwithTheRequest) (i.e. remove the parenthesis) Commented May 22, 2017 at 19:02
  • thanks , that worked ... Commented May 22, 2017 at 19:15
  • Appended an additional question to the previous one ... Commented May 23, 2017 at 9:29

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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().
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().
sorry if i'm being slow but how is that different from an anonymous function ? isn't it being executed immediately ?
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.
Thank you !!! I guess i have to dig some more into js functions and function calls ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.