Skip to content

Instantly share code, notes, and snippets.

@davidgilbertson
Last active December 30, 2019 23:56
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 davidgilbertson/062b648d18cd0e6167e1d84ef9863deb to your computer and use it in GitHub Desktop.
Save davidgilbertson/062b648d18cd0e6167e1d84ef9863deb to your computer and use it in GitHub Desktop.
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