Build a Mini Netflix (with Vue)
  • Introduction
  • Enter Contest
  • Media Delivery and Transformation
  • Setting Up a Webtask Server
  • Provision and Configure a Database
  • Create Server Routes
  • Setting Up a Vue Client
  • Header and Navigation
  • Video Player Component
  • Playing Videos
  • Cloudinary Account and Initialization
  • List of Movies Component
  • Showing A List of Movies
  • New Movie Modal Component
  • Uploading Movies
  • Sharing on Twitter
  • Deploy to Heroku
  • What's Next
Powered by GitBook
On this page
  • Set Up the Middleware
  • Set up Secrets, Bundling, and Watching
  • Create A Movie Route
  • Retrieve All the Movies

Create Server Routes

PreviousProvision and Configure a DatabaseNextSetting Up a Vue Client

Last updated 7 years ago

So far, you have only one index route: Hello World. Create more routes so that you can retrieve all the movies from the database and save them.

Set Up the Middleware

Set up the middleware before all the routes, as follows:

app.use(require('./middleware/db').connectDisconnect);

Set up Secrets, Bundling, and Watching

Recall that you ran the create command on the index.js file earlier. Now perform these tasks:

  1. Bundle two files (index and db).

  2. Specify the secrets (env).

  3. Watch for the changes.

Run this command line for them all:

wt create index --secret MONGO_URL=<MONGOLAB-CONNECTION-URL> --bundle --watch

Grab the URL from the database home:

Create A Movie Route

Add the following route immediately after the / route:

app.post('/movies', (req, res) => {
  const newMovie = new req.movieModal(
    Object.assign(req.body, { created_at: Date.now() })
  );
  newMovie.save((err, savedMovie) => {
    res.json(savedMovie);
  });
});

The above code does the following:

  1. Create a new movie with the movie modal.

  2. Save that movie and return the saved version to the client.

Retrieve All the Movies

Add this route to retrieve all the existing movies:

app.get('/movies', (req, res) => {
  req.movieModal
    .find({})
    .sort({ created_at: -1 })
    .exec((err, movies) => res.json(movies));
});

Subsequently, that route finds the movies, sorts them by date in reverse order, and returns them:

Finally, test with :

Postman