Overview
- already defined mongoose models to interact with the database
- Used to script to create some initial library records.
- Can now write this code to present that information to users
- Determine what information we want to display in our pages
- The define appropriate URLs for returning those resources.
- Then we create routes (URL handlers) and views (templates) to display those pages
- Already created the models
- See the diagram below
- Mostly setting up things to return dummy content in this one.
Routes Primer
- Route is a section of express code that associates GET, POST etc with a URL path and a function to handle that pattern.
- Several ways to create routes. We will use the express.Router middleware.
- Keep our library related routes in a catalog module
- Add routes for handling user accounts/ other functions, we can keep them grouped separately.
Defining and using separate route modules
- Example below of how to create a route module and then use it
// wiki.js - Wiki route module.
var express = require('express');
var router = express.Router();
// Home page route.
router.get('/', function (req, res) {
res.send('Wiki home page');
})
// About page route.
router.get('/about', function (req, res) {
res.send('About this wiki');
})
module.exports = router;
- First, we require express,
- To use it in our main app file, we need to require the route module.
- Then we call Use on it, explaining which path we want to use.
- The two routes get / and get /about are accessible.
var wiki = require('./wiki.js');
// ...
app.use('/wiki', wiki);
Route Functions
- Typical route functions
- About route, for example, uses the GET method – only responds to HTTP Get requests.
- First argument to this method is the URL path, second is the callback that will be invoked if an HTTP GET request with the path is received.
-
router.get(‘/about’, function (req, res) { res.send(‘About this wiki’); })
The callback begins at function. Also anonymous.
Route parameters
- Named URL segments used to capture values at specific positions in the URL.
- Use a colon, and then the name.
- Captured values are stored in the req.params object.
- For example req.params.your_parameter_name
- For example:
http://localhost:3000/users/34/books/8989
- We can extract this information as below:
app.get('/users/:userId/books/:bookId', function (req, res) {
// Access userId via: req.params.userId
// Access bookId via: req.params.bookId
res.send(req.params);
})
Routes needed for the Local Library
- URLs used for our pages are listed below.
- Object – replaced by the name of each of our models.
- catalog/ — The home/index page.
- catalog/<objects>/ — The list of all books, bookinstances, genres, or authors (e.g. /catalog/books/, /catalog/genres/, etc.)
- catalog/<object>/<id> — The detail page for a specific book, bookinstance, genre, or author with the given _id field value (e.g. /catalog/book/584493c1f4887f06c0e67d37).
- catalog/<object>/create — The form to create a new book, bookinstance, genre, or author (e.g. /catalog/book/create).
- catalog/<object>/<id>/update — The form to update a specific book, bookinstance, genre, or author with the given _id field value (e.g. /catalog/book/584493c1f4887f06c0e67d37/update).
- catalog/<object>/<id>/delete — The form to delete a specific book, bookinstance, genre, author with the given _id field value (e.g. /catalog/book/584493c1f4887f06c0e67d37/delete).
- First home page and list pages don’t encode any additional information
- Results returned will depend on the model type and the content in the database.
Create the route-handler callback functions
- Create the skeletons of the callback functions
- Callbacks – stored in separate modules for books, book instances, Games and authors.
/express-locallibrary-tutorial //the project root
/controllers
authorController.js
bookController.js
bookinstanceController.js
genreController.js
- Author Controller
var Author = require('../models/author');
// Display list of all Authors.
exports.author_list = function(req, res) {
res.send('NOT IMPLEMENTED: Author list');
};
// Display detail page for a specific Author.
exports.author_detail = function(req, res) {
res.send('NOT IMPLEMENTED: Author detail: ' + req.params.id);
};
// Display Author create form on GET.
exports.author_create_get = function(req, res) {
res.send('NOT IMPLEMENTED: Author create GET');
};
// Handle Author create on POST.
exports.author_create_post = function(req, res) {
res.send('NOT IMPLEMENTED: Author create POST');
};
// Display Author delete form on GET.
exports.author_delete_get = function(req, res) {
res.send('NOT IMPLEMENTED: Author delete GET');
};
// Handle Author delete on POST.
exports.author_delete_post = function(req, res) {
res.send('NOT IMPLEMENTED: Author delete POST');
};
// Display Author update form on GET.
exports.author_update_get = function(req, res) {
res.send('NOT IMPLEMENTED: Author update GET');
};
// Handle Author update on POST.
exports.author_update_post = function(req, res) {
res.send('NOT IMPLEMENTED: Author update POST');
};
Others are very similar to above.
Update the index route module
- in /routes/index.js:
// GET home page.
router.get('/', function(req, res) {
res.redirect('/catalog');
});
Update app.js
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var 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 the Routes
DEBUG=express-locallibrary-tutorial:* npm start