text: const bodyParser = require('body-parser')
  const express = require('express')
  const server = express()
  
  server.use(bodyParser.json())
  
  let numberOfRequests = 0
  const projects = [{
    id: '1',
    title: 'Iniciar Desafio 01',
    tasks: [
      'Instalar o ExpressJS',
      'Configurar Rota Project'
    ]
  },
  {
    id: '2',
    title: 'Analisar Desafio 01',
    tasks: [
      'Estudar as funçoes slice() find() map()'
    ]
  }]
  
  // Middleware para checar se o projeto existe
  function checkProject (req, res, next) {
    const { id } = req.params
    const project = projects.find(project => project.id === id)
    if (!project) {
      res.status(400).json({ status: 'Project not found!' })
    }
    return next()
  }
  
  /**
   * Middleware que dá log no número de requisições
   */
  function logRequests (req, res, next) {
    numberOfRequests++
    console.log(`Número de requisições: ${numberOfRequests}`)
    return next()
  }
  
  server.use(logRequests)
  
  server.get('/projects', (req, res) => {
    return res.json(projects)
  })
  
  server.post('/projects', (req, res) => {
    projects.push(req.body)
    return res.json({ status: 'Project created successfully' })
  })
  
  server.post('/projects/:id/tasks', checkProject, (req, res) => {
    const { id } = req.params
    const { title } = req.body
    const project = projects.find(project => project.id === id)
    project.tasks.push(title)
    res.json({ status: 'Task created successfully', project })
  })
  
  server.put('/projects/:id', checkProject, (req, res) => {
    const { id } = req.params
    const { title } = req.body
    const project = projects.find(project => project.id === id)
    project.title = title
    return res.json({ status: 'Project updated successfully', project })
  })
  
  server.delete('/projects/:id', checkProject, (req, res) => {
    const { id } = req.params
  
    const projectIndex = projects.findIndex(p => p.id === id)
  
    projects.splice(projectIndex, 1)
  
    return res.send()
  })
  
  // Start Server
  server.listen(4000, () => {
    console.log('Server listening on port 3000')
  })
  