List Matches

To list our matches, we won't need security, because all the users should be able to get the full list of matches in the applications. So let's implement the matches API.

So, create a new file called matches-api.js into the src/routes folder and apply the following code:

const express = require('express')
const api = express.Router()
const Match = require('../models/match')

api
.route('/matches')
.get((req, res, next) => {
Match.find().exec()
.then(matches => res.json(matches))
.catch(err => next(err))
})

module.exports = api

Next, we have to configure the server.js file to map our Match APIs. In the server.js file, apply the following change:

...
const adminApi = require('./src/routes/admin-api')
const matchesApi = require('./src/routes/matches-api')
const mongooseConfig = require('./src/config/mongoose-connection')
const app = express()

app.use(bodyParser.json())
app.use(teamsApi)
app.use(seurityApi)
app.use(adminApi)
app.use(matchesApi)

...

Cool! Let's test things out. Open a Terminal window and execute the following command:

$ curl localhost:3000/matches

[{"_id":"5a949f982c1fda05b8c5c00a","team_1":"Peru","team_2":"Chile","__v":0,"score":{"team_1":20,"team_2":0}}]

That's it! We have our public Match API.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.2.78