Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

292
Vistas
Is it good practice to internally call an API within the server?

I have some codes as follows

// [DELETE] /api/v1/authors/:id
async deleteAuthor(req, res) {
  const author = await Author.findByIdAndRemove(req.params.id);
  // delete blogs of the author
  axios.delete(
    `http://localhost:${process.env.PORT}/api/v1/blogs/author/${author._id}`,
    {
      headers: {
        Authorization: req.headers.authorization
      }
    }
  );
  res.status(200).send();
}

By this, I want to delete an author and all their blogs. I know the naming of the uri is not good but is it overall a good way to code like this or there are other ways to do the same thing. I'm using Node.js and Mongoose

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I think is not a good practice, you should avoid making circular HTTP Calls (Not optimal, can unnecessary duplicate logic, and is more difficult to read your code) What do I suggest? Following DDD & Hex. Architecture:

  1. Have separated services e.g. RemoveAuthor & RemoveAuthorBlogs (Following the SRP principle of SOLID, one service do only one thing)

  2. Your HTTP endpoints (E.g. DELETE /author/:id & DELETE /blogs/author/:id) will invoke those services.

  3. If you need to delete the author and his blogs in the same request.

    a. Create a high service that calls to RemoveAuthor and RemoveAuthorBlogs (E.g. RemoveAuthorReferences)

    b. (My vote is for this) => The service RemoveAuthor remove from DB the author as the first step and as the second step will dispatch a domain event (E.g. AuthorDeleted that will be listened to by an EventHandler (E.g. DeleteBlogsOnAuthorDeleted who will remove the blogs of the author)

In my opinion, your services shouldn't make internal HTTP requests when you can use your own services, in this way, if the logic changes, you'll only need to modify the service

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda