Created
September 13, 2017 11:27
-
-
Save AdamSaleh/ba39c631be904472a73484c8a6eac881 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 PouchDB = require('pouchdb'); | |
const db = new PouchDB('./database'); | |
const express = require('express'); | |
const app = express(); | |
const router = express.Router(); | |
app.use('/locker', router); | |
app.listen(8800, function () { | |
console.log('Listening at http://localhost:8800/locker'); | |
}); | |
router.get('/', (request, response) => { | |
response.status(200).json('Hello'); | |
}); | |
function wrap (responder) { | |
return async (req, res) => { | |
try { | |
await responder(req, res); | |
} catch (exception) { | |
res.status(500).json(exception); | |
} | |
}; | |
} | |
router.post('/', wrap(async (req, res) => { | |
const dbResponse = await db.post({'_attachments': {}}); | |
res.status(200).json(dbResponse); | |
})); | |
router.get('/:id/items', wrap(async (req, res) => { | |
const id = req.params.id; | |
const {_attachments} = await db.get(id); | |
res.status(200).json(Object.keys(_attachments)); | |
})); | |
router.delete('/:id', wrap(async ({params: {id}}, res) => { | |
const doc = await db.get(id); | |
const dbResponse = await db.remove(id, doc._rev); | |
res.json(dbResponse); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment