So I have a question about request from the request module. Is it possible to wrap it in a function and call it later?Also another problem I have is that if my functions are not nested I might not be able to get certain variables especially at the end when the response sends it back to the client side.app.post('/api', (request, responses) => { console.log(request.body); //get town input and job posting const data = request.body; //put data into separate variables const address = data.current; const job_posting = data.next console.log(address); console.log(job_posting) //make a request to job posting website and scrape the address from the job posting requests(`${job_posting}`, (error, response, html) => { if(!error && response.statusCode == 200){ const $ = cheerio.load(html); const siteHeading = $('.jobsearch-JobMetadataHeader-itemWithIcon'); const output = siteHeading.children('span').text(); const job_address = output console.log(job_address) //make a request to the distance api with our address input and the scraped job address requests(`https://www.luftlinie.org/${address}/${job_address}`, (error, response, html) => { if(!error && response.statusCode == 200){ const $ = cheerio.load(html); const distance = $('.headerAirline'); const output_distance = distance.text(); const distance_from_to = output_distance console.log(distance_from_to) //send the data back to display it responses.json( { status: 'success', current: data.current, posted_job: data.next, address_job: job_address, distance: distance_from_to }); } }) } }) }); I want something like this:app.post('/api', (request, responses) => { console.log(request.body); //get town input and job posting const data = request.body; //put data into separate variables const address = data.current; const job_posting = data.next console.log(address); console.log(job_posting) request1() request2() request3() response.json()
Submitted December 02, 2019 at 06:40PM by supergaara000
No comments:
Post a Comment