List of Articles
+-
+ <% for(var i = 0; i < articles.length; i++){ %>
+
- + <%=articles[i].title%> + + <% } %> +
diff --git a/app.js b/app.js index fefbf01..2b42916 100644 --- a/app.js +++ b/app.js @@ -10,6 +10,9 @@ app.use(bodyParser.urlencoded({extended:false})); var authorsController = require('./controllers/authors.js'); app.use('/authors', authorsController); +var articlesController = require('./controllers/articles.js'); +app.use('/articles', articlesController); + app.get('/', function(req, res){ res.render('index.ejs'); }); diff --git a/controllers/articles.js b/controllers/articles.js new file mode 100644 index 0000000..8af4524 --- /dev/null +++ b/controllers/articles.js @@ -0,0 +1,31 @@ +var express = require('express'); +var router = express.Router(); +var Article = require('../models/articles.js'); + +router.get('/', function(req, res){ + Article.find({}, function(err, foundArticles){ + res.render('articles/index.ejs', { + articles: foundArticles + }); + }) +}); + +router.get('/new', function(req, res){ + res.render('articles/new.ejs'); +}); + +router.post('/', function(req, res){ + Article.create(req.body, function(err, createdArticle){ + res.redirect('/articles'); + }) +}); + +router.get('/:id', function(req,res){ + Article.findById(req.params.id, function(err, foundArticle){ + res.render('articles/show.ejs', { + article: foundArticle + }); + }); +}); + +module.exports = router; diff --git a/models/articles.js b/models/articles.js new file mode 100644 index 0000000..7dbf681 --- /dev/null +++ b/models/articles.js @@ -0,0 +1,10 @@ +var mongoose = require('mongoose'); + +var articleSchema = mongoose.Schema({ + title:String, + body:String +}); + +var Article = mongoose.model('Article', articleSchema); + +module.exports = Article; diff --git a/views/articles/index.ejs b/views/articles/index.ejs new file mode 100644 index 0000000..a29eb71 --- /dev/null +++ b/views/articles/index.ejs @@ -0,0 +1,32 @@ + + +
+ +