Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
POST Request to PUT Request

" An open source course that I am taking has the following prompt for fullstack development: 3.17: Phonebook database, step5 If the user tries to create a new phonebook entry for a person whose name is already in the phonebook, the frontend will try to update the phone number of the existing entry by making an HTTP PUT request to the entry's unique URL. Modify the backend to support this request. Verify that the frontend works after making your changes."*

It appears that this would call for an express app.put request called inside on an app.post request. Is there something that I'm missing here? How you handle this kind of logic with mongoDB/mongoose/expresss? My current post code is pasted below.

Thanks

app.post('/api/persons',(req,res) => {
  const body = req.body
  const containsNameNumber = body.name && body.number

  if(!containsNameNumber){
    return res.status(400).json({
      error:"must specify a name and a number"
    })
  }

  const phone = new Person({
    name: body.name,
    number:body.number
  })

  phone.save().then(savedPerson => {
      res.json(savedPerson)
    })
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you are missunderstanding one thing, check PUT definition from RFC7231

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload

Is importante the part "be created". So with a PUT if the resource exists it will be updated but if not exists will be created.

If the target resource does not have a current representation and the PUT successfully creates one, then the origin server MUST inform the user agent by sending a 201 (Created) response.

So you only have to do a PUT call and check if exists to update (return 200 Ok) or not exists to create (return 201 Created).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda