Skip to content

Instantly share code, notes, and snippets.

@itto-ki
Last active July 7, 2018 20:59
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 itto-ki/1b2b67b340ccac544c9900a57235e7c6 to your computer and use it in GitHub Desktop.
Save itto-ki/1b2b67b340ccac544c9900a57235e7c6 to your computer and use it in GitHub Desktop.
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