Last active
December 30, 2019 23:56
-
-
Save davidgilbertson/062b648d18cd0e6167e1d84ef9863deb to your computer and use it in GitHub Desktop.
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
const express = require('express'); | |
const database = require('./database'); | |
const app = express(); | |
app.post('/', async (req, res) => { | |
const id = await database.create(req.body); | |
res.json({id}); | |
}); | |
app.get('/:id', async (req, res) => { | |
const data = await database.read(req.params.id); | |
if (data) { | |
res.json({data}); | |
} else { | |
res.status(404).json({error: 'No item with that ID exists'}); | |
} | |
}); | |
app.put('/:id', async (req, res) => { | |
const success = await database.update(req.params.id, req.body); | |
if (success) { | |
res.json({success: 'Item updated'}); | |
} else { | |
res.status(404).json({error: 'No item with that ID exists'}); | |
} | |
}); | |
app.delete('/:id', async (req, res) => { | |
await database.delete(req.params.id); | |
res.json({success: 'Item deleted'}); | |
}); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment