commit
51ebfd25ca
@ -0,0 +1 @@
|
|||||||
|
node_modules
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var app = express();
|
||||||
|
var bodyParser = require('body-parser');
|
||||||
|
var mongoose = require('mongoose');
|
||||||
|
var methodOverride = require('method-override')
|
||||||
|
|
||||||
|
app.use(methodOverride('_method'));
|
||||||
|
app.use(bodyParser.urlencoded({extended:false}));
|
||||||
|
|
||||||
|
var authorsController = require('./controllers/authors.js');
|
||||||
|
app.use('/authors', authorsController);
|
||||||
|
|
||||||
|
app.get('/', function(req, res){
|
||||||
|
res.render('index.ejs');
|
||||||
|
});
|
||||||
|
|
||||||
|
mongoose.connect('mongodb://localhost:27017/blog');
|
||||||
|
|
||||||
|
mongoose.connection.once('open', function(){
|
||||||
|
console.log('connected to mongo');
|
||||||
|
});
|
||||||
|
app.listen(3000, function(){
|
||||||
|
console.log('listening....');
|
||||||
|
});
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
var express = require('express');
|
||||||
|
var Author = require('../models/authors.js');
|
||||||
|
var router = express.Router();
|
||||||
|
|
||||||
|
router.get('/', function(req, res){
|
||||||
|
Author.find({}, function(err, foundAuthors){
|
||||||
|
res.render('authors/index.ejs', {
|
||||||
|
authors: foundAuthors
|
||||||
|
});
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/', function(req, res){
|
||||||
|
Author.create(req.body, function(err, createdAuthor){
|
||||||
|
res.redirect('/authors');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/new', function(req, res){
|
||||||
|
res.render('authors/new.ejs');
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/:id', function(req, res){
|
||||||
|
Author.findById(req.params.id, function(err, foundAuthor){
|
||||||
|
res.render('authors/show.ejs', {
|
||||||
|
author: foundAuthor
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.delete('/:id', function(req, res){
|
||||||
|
Author.findByIdAndRemove(req.params.id, function(){
|
||||||
|
res.redirect('/authors');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.put('/:id', function(req, res){
|
||||||
|
Author.findByIdAndUpdate(req.params.id, req.body, function(){
|
||||||
|
res.redirect('/authors');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/:id/edit', function(req, res){
|
||||||
|
Author.findById(req.params.id, function(err, foundAuthor){
|
||||||
|
res.render('authors/edit.ejs', {
|
||||||
|
author: foundAuthor
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
var mongoose = require('mongoose');
|
||||||
|
|
||||||
|
var authorSchema = mongoose.Schema({
|
||||||
|
name: String
|
||||||
|
});
|
||||||
|
|
||||||
|
var author = mongoose.model('Author', authorSchema);
|
||||||
|
|
||||||
|
module.exports = author;
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"name": "blog",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC"
|
||||||
|
}
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>Edit <%=author.name%>'s Info</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/">Home</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/authors">Create a new Author</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
<main>
|
||||||
|
<h2>Author Attributes:</h2>
|
||||||
|
<form action="/authors/<%=author._id%>?_method=PUT" method="post">
|
||||||
|
<input type="text" name="name" value="<%=author.name%>"/>
|
||||||
|
<input type="submit" value="Update Author"/>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</header>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,32 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>Authors</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/">Home</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/authors/new">Create a new Author</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<h2>List of Authors</h2>
|
||||||
|
<ul>
|
||||||
|
<% for(var i = 0; i < authors.length; i++){ %>
|
||||||
|
<li>
|
||||||
|
<a href="/authors/<%=authors[i]._id%>"><%=authors[i].name%></a>
|
||||||
|
</li>
|
||||||
|
<% } %>
|
||||||
|
</ul>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>Create an Author</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/">Home</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/authors">Authors Index</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
<main>
|
||||||
|
<form action="/authors" method="post">
|
||||||
|
<input type="text" name="name" />
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>Show Page for <%=author.name%></h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/">Home</a>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href="/authors">Create a new Author</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
<main>
|
||||||
|
<section>
|
||||||
|
<h2>Author Attributes:</h2>
|
||||||
|
<ul>
|
||||||
|
<li>Name: <%=author.name%></li>
|
||||||
|
</ul>
|
||||||
|
<a href="/authors/<%=author._id%>/edit">Edit</a>
|
||||||
|
<form action="/authors/<%=author._id%>?_method=DELETE" method="post">
|
||||||
|
<input type="submit" value="Delete Author"/>
|
||||||
|
</form>
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
</header>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>Welcome to the Blog</h1>
|
||||||
|
<nav>
|
||||||
|
<ul>
|
||||||
|
<li>
|
||||||
|
<a href="/authors">Authors</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Reference in new issue