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