Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
¿Cuál es la forma correcta de declarar un objeto en el enrutador Node.js?

Estoy tratando de resolver las fugas de memoria en mi aplicación Node.js y parece que este código se filtra

 const ApolloClient = require('apollo-client').ApolloClient; const fetch = require('node-fetch'); const createHttpLink = require('apollo-link-http').createHttpLink; const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache; const httpLink = createHttpLink({ uri: 'http://xxxxxx', fetch: fetch }); const client = new ApolloClient({ link: httpLink, cache: new InMemoryCache() }); module.exports = (app) => { app.post('/graphql', async (req, res, next) => { try { const data = await client.query({ query: 'xxxxxxx', variables: 'xxxxxxx' }); return res.json(data); } catch (err) { return next('error'); } }); };

entonces el cliente ApolloClient se recrea cada vez ya que es global. ¿Es mejor definirlo dentro de la ruta? ¿Entonces no causará problemas de rendimiento?

 const ApolloClient = require('apollo-client').ApolloClient; const fetch = require('node-fetch'); const createHttpLink = require('apollo-link-http').createHttpLink; const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache; module.exports = (app) => { app.post('/graphql', async (req, res, next) => { try { let httpLink = createHttpLink({ uri: 'http://xxxxxx', fetch: fetch }); let client = new ApolloClient({ link: httpLink, cache: new InMemoryCache() }); const data = await client.query({ query: 'xxxxxxx', variables: 'xxxxxxx' }); httpLink = null client = null return res.json(data); } catch (err) { return next('error'); } }); };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Crear una instancia de ApolloClient y reutilizarla es mejor por muchas razones.

  1. Número mínimo de instancias.

Considere tener 20 o más (N) endpoints/middlewares que necesitan acceder a algunos datos almacenados en su base de datos. Crear una instancia de ApolloClient para cada uno significaría un número N de instancias. Esto no es eficaz y viola y viola la regla de codificación 'No repetirse'.

Por otro lado, crear una instancia y usarla en toda la aplicación es muy útil. Considere este ejemplo:

 const ApolloClient = require('apollo-client').ApolloClient; const fetch = require('node-fetch'); const createHttpLink = require('apollo-link-http').createHttpLink; const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache; const httpLink = createHttpLink({ uri: 'http://xxxxxx', fetch: fetch }); // instantiating module.exports = new ApolloClient({ link: httpLink, cache: new InMemoryCache() });

un módulo:

 const dbClient = require('./db_client') module.exports = (app) => { app.post('/graphql', async (req, res, next) => { try { const data = await dbClient.query({ query: 'xxxxxxx', variables: 'xxxxxxx' }); return res.json(data); } catch (err) { return next('error'); } }); };

otro modulo:

 const dbClient = require('./db_client') ...

otro modulo:

 const dbClient = require('./db_client') ...

Como se puede ver, con solo una instancia podemos lograr una aplicación más robusta y con mayor rendimiento.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!