Last active
December 11, 2018 19:22
-
-
Save madebycaliper/3b38ec52811414eb8267b72d187069e5 to your computer and use it in GitHub Desktop.
Async Node Response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./routes/api/index.js | |
// the router is already using /api (see routes/index.js), so this is actually a post to /api/ | |
router.post('/', function(req, res) { | |
// we have to wrap our *await* commands in | |
// an async function for them to work | |
(async () => { | |
// Generate the PDF | |
. | |
. | |
. | |
await page.pdf(PDFOptions) | |
// Upload to t | |
dbx.filesUpload({ path: dbxPath, contents, mode: 'overwrite' }) | |
.then(response => { | |
// NEW RETURN when promise resolves (is that what's happening?) | |
// Send Dropbox response back with status | |
return res.status(200).send(response) | |
}) | |
.catch( err => res.sendStatus(500) ) | |
})() | |
// "Always return Status 200/ok" | |
// DEPRECATED | |
// res.sendStatus(200) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./routes/index.js | |
// for testing the POST API request | |
router.get('/test@togo', (req, res) => { | |
// dummy data | |
let data = {TEST_DATA_FOR_TOGO_MENU} | |
// Return the response of the test request to the client | |
axios.post('http://localhost:3000/api', data) | |
.then(response => res.status(200).json(response.data) ) | |
.catch(err => res.sendStatus(err.status || err.response) ) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment