Skip to content

Instantly share code, notes, and snippets.

@madebycaliper
Last active December 11, 2018 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madebycaliper/3b38ec52811414eb8267b72d187069e5 to your computer and use it in GitHub Desktop.
Save madebycaliper/3b38ec52811414eb8267b72d187069e5 to your computer and use it in GitHub Desktop.
Async Node Response
// ./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)
})
// ./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