Nodejs express calling function.

9 views
Skip to first unread message

amir....@nurun.com

unread,
May 19, 2017, 11:28:46 PM5/19/17
to nodejs
Hi, there is this following code which I'd like to bring the request part out of "app.post", store it in a separate file and call it whenever needed.

I have tried the export method but I was keep getting the undefined for "req" and "res".

app.post('/hook', function (req, res) {
    url
= "https://MY_URL",


   
// ( request part )
   
// I'd like to store following to a separate file
   
// and call it from main file whenever needed.


    request
({url : url, headers : {"Authorization" : auth}},
   
function (error, response, body) {
        parseString
(body, function (err, result) {
           
try {
               
return res.json({
                   
"SOME_JSON": "JSON"
               
});
           
} catch (err) {
                console
.error("no request!", err);
           
}
       
});
   
});
   
// end request
});


a help on this would be greatly appreciated.


Reply all
Reply to author
Forward
0 new messages