parent
51ebfd25ca
commit
935c51d7db
@ -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;
|
||||||
@ -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;
|
||||||
@ -0,0 +1,32 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>Articles</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/">Home</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/articles/new">Create a new article</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<h2>List of Articles</h2>
|
||||||
|
<ul>
|
||||||
|
<% for(var i = 0; i < articles.length; i++){ %>
|
||||||
|
<li>
|
||||||
|
<a href="/articles/<%=articles[i]._id%>"><%=articles[i].title%></a>
|
||||||
|
</li>
|
||||||
|
<% } %>
|
||||||
|
</ul>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>Create an Article</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/">Home</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/articles">Articles Index</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<form action="/articles" method="post">
|
||||||
|
<input type="text" name="title" /><br/>
|
||||||
|
<textarea name="body"></textarea><br/>
|
||||||
|
<input type="submit" value="Publish Article"/>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,25 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1><%=article.title%></h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/">Home</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/articles">Articles Index</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<%=article.body%>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Reference in new issue