Last active
January 16, 2016 20:09
-
-
Save sea-witch/35c85f31aee76be4d382 to your computer and use it in GitHub Desktop.
CRUD routes in ExpressFrom http://aleksandrov.ws/2013/09/12/restful-api-with-nodejs-plus-mongodb/
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
var express = require('express'); | |
var router = express.Router(); | |
router.get('/', function(req, res) { | |
res.send('READ all items'); | |
}); | |
router.post('/new', function(req, res) { | |
var data = req.body | |
var response = '' | |
for (item in data) { | |
response += item, | |
response += ': ' | |
response += data[item] | |
response += ', ' | |
} | |
console.log(data) | |
res.send('CREATE a new item with this data: ' + response); | |
}); | |
router.get('/:id', function(req, res) { | |
var id = req.params.id | |
res.send('READ item ' + id); | |
}); | |
router.put('/:id', function (req, res){ | |
var id = req.params.id | |
res.send('UPDATE item ' + id); | |
}); | |
router.delete('/:id', function (req, res){ | |
var id = req.params.id | |
res.send('DELETE item ' + id); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment