Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var express = require('express');
var router = express.Router();
var ArticleModel = require('../db/articleModel.js');
router.get('/', function(req, res, next) {
ArticleModel
.find()
.then(function(users) {
res.json(users);
});
});
router.post('/', function(req, res, next) {
var Article = new ArticleModel();
Article.title = req.body.title;
Article.content = req.body.content;
Article.save(function(err) {
if(err){
res.send(err);
} else {
res.json({message: "success!"});
}
});
});
router.get('/:id', function(req, res, next) {
ArticleModel
.findById(req.params.id, function(err, article) {
if(err) {
res.send(err);
}
res.json(article);
});
})
router.put('/:id', function(req, res, next) {
ArticleModel
.findById(req.params.id, function(err, article) {
article.title = req.body.title;
article.content = req.body.content;
article.last_modified_at = Date.now();
article.save(function(err) {
if(err) {
res.send(err);
}
res.json({message: "success!"});
});
});
});
router.delete('/:id', function(req, res, next) {
ArticleModel
.findById(req.params.id, function(err, article) {
article.remove(function(err) {
if(err) {
res.send(err);
}
res.json({message: "success!"});
});
});
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment