NodeJs / Basics / Controller
Controller
-
STEP
Folder structure
Author controller/controllers authorController.js bookController.js bookinstanceController.js genreController.js
BookInstance controllerconst Author = require("../models/author"); // Display list of all Authors. exports.author_list = (req, res) => { res.send("NOT IMPLEMENTED: Author list"); }; // Display detail page for a specific Author. exports.author_detail = (req, res) => { res.send(`NOT IMPLEMENTED: Author detail: ${req.params.id}`); }; // Display Author create form on GET. exports.author_create_get = (req, res) => { res.send("NOT IMPLEMENTED: Author create GET"); }; // Handle Author create on POST. exports.author_create_post = (req, res) => { res.send("NOT IMPLEMENTED: Author create POST"); }; // Display Author delete form on GET. exports.author_delete_get = (req, res) => { res.send("NOT IMPLEMENTED: Author delete GET"); }; // Handle Author delete on POST. exports.author_delete_post = (req, res) => { res.send("NOT IMPLEMENTED: Author delete POST"); }; // Display Author update form on GET. exports.author_update_get = (req, res) => { res.send("NOT IMPLEMENTED: Author update GET"); }; // Handle Author update on POST. exports.author_update_post = (req, res) => { res.send("NOT IMPLEMENTED: Author update POST"); };
Genre controllerconst BookInstance = require("../models/bookinstance"); // Display list of all BookInstances. exports.bookinstance_list = (req, res) => { res.send("NOT IMPLEMENTED: BookInstance list"); }; // Display detail page for a specific BookInstance. exports.bookinstance_detail = (req, res) => { res.send(`NOT IMPLEMENTED: BookInstance detail: ${req.params.id}`); }; // Display BookInstance create form on GET. exports.bookinstance_create_get = (req, res) => { res.send("NOT IMPLEMENTED: BookInstance create GET"); }; // Handle BookInstance create on POST. exports.bookinstance_create_post = (req, res) => { res.send("NOT IMPLEMENTED: BookInstance create POST"); }; // Display BookInstance delete form on GET. exports.bookinstance_delete_get = (req, res) => { res.send("NOT IMPLEMENTED: BookInstance delete GET"); }; // Handle BookInstance delete on POST. exports.bookinstance_delete_post = (req, res) => { res.send("NOT IMPLEMENTED: BookInstance delete POST"); }; // Display BookInstance update form on GET. exports.bookinstance_update_get = (req, res) => { res.send("NOT IMPLEMENTED: BookInstance update GET"); }; // Handle bookinstance update on POST. exports.bookinstance_update_post = (req, res) => { res.send("NOT IMPLEMENTED: BookInstance update POST"); };
Book controllerconst Genre = require("../models/genre"); // Display list of all Genre. exports.genre_list = (req, res) => { res.send("NOT IMPLEMENTED: Genre list"); }; // Display detail page for a specific Genre. exports.genre_detail = (req, res) => { res.send(`NOT IMPLEMENTED: Genre detail: ${req.params.id}`); }; // Display Genre create form on GET. exports.genre_create_get = (req, res) => { res.send("NOT IMPLEMENTED: Genre create GET"); }; // Handle Genre create on POST. exports.genre_create_post = (req, res) => { res.send("NOT IMPLEMENTED: Genre create POST"); }; // Display Genre delete form on GET. exports.genre_delete_get = (req, res) => { res.send("NOT IMPLEMENTED: Genre delete GET"); }; // Handle Genre delete on POST. exports.genre_delete_post = (req, res) => { res.send("NOT IMPLEMENTED: Genre delete POST"); }; // Display Genre update form on GET. exports.genre_update_get = (req, res) => { res.send("NOT IMPLEMENTED: Genre update GET"); }; // Handle Genre update on POST. exports.genre_update_post = (req, res) => { res.send("NOT IMPLEMENTED: Genre update POST"); }; const Book = require("../models/book"); exports.index = (req, res) => { res.send("NOT IMPLEMENTED: Site Home Page"); }; // Display list of all books. exports.book_list = (req, res) => { res.send("NOT IMPLEMENTED: Book list"); }; // Display detail page for a specific book. exports.book_detail = (req, res) => { res.send(`NOT IMPLEMENTED: Book detail: ${req.params.id}`); }; // Display book create form on GET. exports.book_create_get = (req, res) => { res.send("NOT IMPLEMENTED: Book create GET"); }; // Handle book create on POST. exports.book_create_post = (req, res) => { res.send("NOT IMPLEMENTED: Book create POST"); }; // Display book delete form on GET. exports.book_delete_get = (req, res) => { res.send("NOT IMPLEMENTED: Book delete GET"); }; // Handle book delete on POST. exports.book_delete_post = (req, res) => { res.send("NOT IMPLEMENTED: Book delete POST"); }; // Display book update form on GET. exports.book_update_get = (req, res) => { res.send("NOT IMPLEMENTED: Book update GET"); }; // Handle book update on POST. exports.book_update_post = (req, res) => { res.send("NOT IMPLEMENTED: Book update POST"); }; Create the catalog route module
/routes index.js users.js catalog.js Open /routes/catalog.js and copy in the code below:
const express = require("express"); const router = express.Router(); // Require controller modules. const book_controller = require("../controllers/bookController"); const author_controller = require("../controllers/authorController"); const genre_controller = require("../controllers/genreController"); const book_instance_controller = require("../controllers/bookinstanceController"); /// BOOK ROUTES /// // GET catalog home page. router.get("/", book_controller.index); // GET request for creating a Book. NOTE This must come before routes that display Book (uses id). router.get("/book/create", book_controller.book_create_get); // POST request for creating Book. router.post("/book/create", book_controller.book_create_post); // GET request to delete Book. router.get("/book/:id/delete", book_controller.book_delete_get); // POST request to delete Book. router.post("/book/:id/delete", book_controller.book_delete_post); // GET request to update Book. router.get("/book/:id/update", book_controller.book_update_get); // POST request to update Book. router.post("/book/:id/update", book_controller.book_update_post); // GET request for one Book. router.get("/book/:id", book_controller.book_detail); // GET request for list of all Book items. router.get("/books", book_controller.book_list); /// AUTHOR ROUTES /// // GET request for creating Author. NOTE This must come before route for id (i.e. display author). router.get("/author/create", author_controller.author_create_get); // POST request for creating Author. router.post("/author/create", author_controller.author_create_post); // GET request to delete Author. router.get("/author/:id/delete", author_controller.author_delete_get); // POST request to delete Author. router.post("/author/:id/delete", author_controller.author_delete_post); // GET request to update Author. router.get("/author/:id/update", author_controller.author_update_get); // POST request to update Author. router.post("/author/:id/update", author_controller.author_update_post); // GET request for one Author. router.get("/author/:id", author_controller.author_detail); // GET request for list of all Authors. router.get("/authors", author_controller.author_list); /// GENRE ROUTES /// // GET request for creating a Genre. NOTE This must come before route that displays Genre (uses id). router.get("/genre/create", genre_controller.genre_create_get); //POST request for creating Genre. router.post("/genre/create", genre_controller.genre_create_post); // GET request to delete Genre. router.get("/genre/:id/delete", genre_controller.genre_delete_get); // POST request to delete Genre. router.post("/genre/:id/delete", genre_controller.genre_delete_post); // GET request to update Genre. router.get("/genre/:id/update", genre_controller.genre_update_get); // POST request to update Genre. router.post("/genre/:id/update", genre_controller.genre_update_post); // GET request for one Genre. router.get("/genre/:id", genre_controller.genre_detail); // GET request for list of all Genre. router.get("/genres", genre_controller.genre_list); /// BOOKINSTANCE ROUTES /// // GET request for creating a BookInstance. NOTE This must come before route that displays BookInstance (uses id). router.get( "/bookinstance/create", book_instance_controller.bookinstance_create_get ); // POST request for creating BookInstance. router.post( "/bookinstance/create", book_instance_controller.bookinstance_create_post ); // GET request to delete BookInstance. router.get( "/bookinstance/:id/delete", book_instance_controller.bookinstance_delete_get ); // POST request to delete BookInstance. router.post( "/bookinstance/:id/delete", book_instance_controller.bookinstance_delete_post ); // GET request to update BookInstance. router.get( "/bookinstance/:id/update", book_instance_controller.bookinstance_update_get ); // POST request to update BookInstance. router.post( "/bookinstance/:id/update", book_instance_controller.bookinstance_update_post ); // GET request for one BookInstance. router.get("/bookinstance/:id", book_instance_controller.bookinstance_detail); // GET request for list of all BookInstance. router.get("/bookinstances", book_instance_controller.bookinstance_list); module.exports = router; Update the index route module
Open /routes/index.js
router.get("/", function (req, res) { res.redirect("/catalog"); }); Update app.js
Open app.js
const indexRouter = require("./routes/index"); const usersRouter = require("./routes/users"); const catalogRouter = require("./routes/catalog"); //Import routes for "catalog" area of site app.use("/", indexRouter); app.use("/users", usersRouter); app.use("/catalog", catalogRouter); // Add catalog routes to middleware chain. testing
http://localhost:3000/ http://localhost:3000/catalog http://localhost:3000/catalog/books http://localhost:3000/catalog/bookinstances/ http://localhost:3000/catalog/authors/ http://localhost:3000/catalog/genres/ http://localhost:3000/catalog/book/5846437593935e2f8c2aa226 http://localhost:3000/catalog/book/create