Skip to content

Instantly share code, notes, and snippets.

@eduardoromero
Last active September 24, 2017 18:56
Show Gist options
  • Save eduardoromero/043dd254836e9135910573a781e2e7d2 to your computer and use it in GitHub Desktop.
Save eduardoromero/043dd254836e9135910573a781e2e7d2 to your computer and use it in GitHub Desktop.
Serverless WebTask API - POST/PUT
server.post('/', (req, res) => {
let data = req.body
req.db.table('shares').insert(data, {returnChanges: true})
.then((response) => {
data = Array.isArray(req.body) ? response.changes.map(item => item.new_val) : response.changes[0].new_val
res.status(200).json({
data: data,
response
})
})
.catch(error => res.status(500).json({error}))
})
server.put('/', (req, res) => {
let data = req.body
req.db.table('shares').insert(data, {conflict: 'update', returnChanges: true})
.then((response) => {
data = Array.isArray(req.body) ? response.changes.map(item => item.new_val) : response.changes[0].new_val
res.status(200).json({
data: data,
response
})
})
.catch(error => res.status(500).json({error}))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment