Full-Stack Web Development with Vue.js and Node

(singke) #1
Building the Real Application Chapter 5

Until now, we have our frontend server up and running on port 8080 with the following:


$ npm run dev

The backend server running on port 8081 with the following:


$ node server.js

One important thing to remember is that whenever we change the code in server.js, we


have to restart the server by running the following command:


$ node server.js

This is a very tedious task. However, there is one good way to get rid of that. There is a


package called nodemon, which, when installed, restarts the server automatically whenever


the code gets updated and we don't have to do it manually every time. So, let's go ahead


and install the package:


$ npm install nodemon --save

With the package installed, now we can start our server with the following command:


$ nodemon server.js

Adding a Movie model


The next thing is to add the movies to the database when we submit the form. Let's go
ahead and create a folder called models in the root directory and add a Movie.js file in


the models directory:


We will be using singular capitalized names for Models, and all lowercase
plural names for Controllers files.

The following code into the Movie.js:


const mongoose = require('mongoose');

const Schema = mongoose.Schema;
const MovieSchema = new Schema({
name: String,
description: String,
release_year: Number,
genre: String,
Free download pdf