O'Reilly logo

Hands-On Full Stack Web Development with Aurelia by Erikson Haziz Murrugarra Sifuentes, Diego Jose Arguelles Rojas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a new team

To create a new team, we need to call the model's built-in methods provided by Mongoose. The save function is used to create and update the fields for any model. So, first we will start importing the Team model into the src/routes/teams-api.js file, as follows:

const express = require('express')const api = express.Router()const Team = require('../models/team')let teams = [    { id: 1, name: "Peru"},    { id: 2, name: "Russia"}]...

Now that we have imported the module with the require function and stored it into the Team constant, we can use it to create a new team. Let's modify the POST HTTP method of the Rest Controller:

...api  .route('/teams')  .get((req, res) => {    res.json(teams)  })  .post((req, res, next) => {    let team = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required