Building the Real Application Chapter 5
{
path: '/contact',
name: 'Contact',
component: Contact,
},
{
path: '/movies/add',
name: 'AddMovie',
component: AddMovie,
},
{
path: '/movies/:id',
name: 'Movie',
component: Movie,
},
],
});
Now, we need to add an endpoint for a GET request to fetch the movie with the specified
ID.
Replace the content in controllers/movies.js with the following:
const MovieSchema = require('../models/Movie.js');
module.exports.controller = (app) => {
// fetch all movies
app.get('/movies', (req, res) => {
MovieSchema.find({}, 'name description release_year genre', (error,
movies) => {
if (error) { console.log(error); }
res.send({
movies,
});
});
});
// fetch a single movie
app.get('/api/movies/:id', (req, res) => {
MovieSchema.findById(req.params.id, 'name description release_year
genre', (error, movie) => {
if (error) { console.error(error); }
res.send(movie);
});
});
// add a new movie