Last active
September 24, 2017 18:56
-
-
Save eduardoromero/043dd254836e9135910573a781e2e7d2 to your computer and use it in GitHub Desktop.
Serverless WebTask API - POST/PUT
This file contains 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
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