Created
January 16, 2018 21:23
-
-
Save esslamben/65174f2f935f1d25f580f4a1894db8d3 to your computer and use it in GitHub Desktop.
Test
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 mongoose = require('mongoose'); | |
const Compressor = mongoose.model('compressor'); | |
module.exports = app => { | |
const uri = '/api/compressor'; | |
// Gets all compressors | |
app.get(uri, async (req, res) => { | |
const compressors = await Compressor.find(); | |
res.send(compressors); | |
}); | |
// Post compressor | |
app.post(uri, async (req, res) => { | |
const details = req.body; | |
const compressor = new Compressor({ | |
name: 'test123' | |
}); | |
try { | |
await compressor.save(); | |
res.send('Compressor Created!'); | |
} catch (err) { | |
res.status(422).send(err); | |
} | |
}); | |
// Delete compressor | |
app.get(`${uri}/:id`, async (req, res) => { | |
res.send(req.params.id); | |
try { | |
Compressor.findByIdAndRemove(req.params.id); | |
res.send('Compressor Deleted!'); | |
} catch (err) { | |
res.status(422).send(err); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment