Two Model CRUD App - No relationship - First Model
#
Lesson Objectives- Init Directory
- Start express
- Create Home page
- Create Authors Index
- Create Authors New Page
- Set up Author Model
- Create Authors Post Route
- Show Authors on Index Page
- Create Authors Show Page
- Create Authors Delete Route
- Create Authors Edit Page
- Create Authors Put Route
#
Init Directorymkdir student_examples/blog
cd student_examples/blog
npm init
- make entry point
server.js
- make entry point
npm install express --save
touch server.js
#
Start expressserver.js:
const express = require("express");const app = express();
app.listen(3000, () => { console.log("listening....");});
#
Create Home pagenpm install ejs --save
mkdir views
touch views/index.ejs
views/index.ejs:
<!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> <li> <a href="/articles">Articles</a> </li> </ul> </nav> </header> </body></html>
server.js:
app.get("/", (req, res) => { res.render("index.ejs");});
#
Create Authors Indexmkdir views/authors
touch views/authors/index.ejs
views/authors.ejs:
<!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> </body></html>
mkdir controllers
touch controllers/authors.js
controllers/authors.js:
const express = require("express");const router = express.Router();
router.get("/", (req, res) => { res.render("authors/index.ejs");});
module.exports = router;
Use the controller in server.js:
const authorsController = require("./controllers/authors.js");app.use("/authors", authorsController);
#
Create Authors New Pagetouch views/authors/new.ejs
<!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>
create route in controllers/authors.js
router.get("/new", (req, res) => { res.render("authors/new.ejs");});
#
Connect to mongonpm install mongoose --save
- Connect in server.js
const mongoose = require("mongoose");//...//...farther down the pagemongoose.connect("mongodb://localhost:27017/blog");
mongoose.connection.once("open", () => { console.log("connected to mongo");});
#
Set up Author Modelmkdir models
touch models/authors.js
const mongoose = require("mongoose");
const authorSchema = mongoose.Schema({ name: String,});
const Author = mongoose.model("Author", authorSchema);
module.exports = Author;
#
Create Authors Create Route- use body parser in server.js
app.use(express.urlencoded({ extended: false }));
controllers/authors.js
const Author = require("../models/authors.js");//...//...farther down the pagerouter.post("/", (req, res) => { Author.create(req.body, (err, createdAuthor) => { res.redirect("/authors"); });});
#
Show Authors on Index Pagecontrollers/authors.js:
router.get("/", (req, res) => { Author.find({}, (err, foundAuthors) => { res.render("authors/index.ejs", { authors: foundAuthors, }); });});
views/authors/index.ejs:
<main> <h2>List of Authors</h2> <ul> <% for(let i = 0; i < authors.length; i++){ %> <li> <a href="/authors/<%=authors[i]._id%>"><%=authors[i].name%></a> </li> <% } %> </ul></main>
#
Create Authors Show Pagetouch views/authors/show.ejs
<!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">Author Index</a> </li> </ul> </nav> </header> <main> <section> <h2>Author Attributes:</h2> <ul> <li>Name: <%=author.name%></li> </ul> </section> </main> </body></html>
towards the bottom controllers/authors.js:
//avoid this handling /new by placing it towards the bottom of the filerouter.get("/:id", (req, res) => { Author.findById(req.params.id, (err, foundAuthor) => { res.render("authors/show.ejs", { author: foundAuthor, }); });});
#
Create Authors Delete Routenpm install method-override --save
- use method-override in server.js:
const methodOverride = require('method-override')
app.use(methodOverride('_method'));
controllers/authors.js:
router.delete("/:id", (req, res) => { Author.findByIdAndRemove(req.params.id, () => { res.redirect("/authors"); });});
views/authors/show.ejs
<section> <form action="/authors/<%=author._id%>?_method=DELETE" method="post"> <input type="submit" value="Delete Author" /> </form></section>
#
Create Authors Edit PageCreate a link on views/authors/show.ejs:
<section> <a href="/authors/<%=author._id%>/edit">Edit</a></section>
controllers/authors.js
router.get("/:id/edit", (req, res) => { Author.findById(req.params.id, (err, foundAuthor) => { res.render("authors/edit.ejs", { author: foundAuthor, }); });});
touch views/authors/edit.ejs
<!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">Authors Index</a> </li> </ul> </nav> </header> <main> <h2>Author Attributes:</h2> <form action="/authors/<%=author._id%>?_method=PUT" method="post"> <input type="text" name="name" value="<%=author.name%>" /><br /> <input type="submit" value="Update Author" /> </form> </main> </body></html>
#
Create Authors Put Routecontrollers/authors.js:
router.put("/:id", (req, res) => { Author.findByIdAndUpdate(req.params.id, req.body, () => { res.redirect("/authors"); });});