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

247
Views
NODEJS, ¿cómo exporto correctamente este método de mi clase Container a otro archivo JS donde manejo express?

[Estoy tratando de pasar mi método getAll() de mi clase Container que se encuentra en app.js a mi archivo appServer.js para pasarlo a res.send() de mi variable app = express() y pasar esa res. enviar() a la ruta "/productos"

cuando trato de ejecutar el servidor me arroja un error que me dice que Container.getAll() no es una función

Intenté exportar usando export.getAll() pero todavía me arroja un error]

[Aplicación.js]

 const fs = require("fs"); class Contenedor { constructor(txtNameFile) { this.txtNameFile = txtNameFile; this.products = []; } async fileInJSON() { let fileTxt = await fs.promises.readFile(this.txtNameFile, "utf-8"); let type = JSON.parse(fileTxt); return type; } async fileSaving(item) { let type = JSON.stringify(item); await fs.promises.writeFile(this.txtNameFile, type); } async save(obj) { try { let fileTxt = await fs.promises.readFile(this.txtNameFile, "utf-8"); if (fileTxt === "") { obj.id = 1; this.products.push(obj); } else { const type = JSON.parse(fileTxt); obj.id = type[type.length - 1].id + 1; type.push(obj); this.products = type; this.fileSaving(type); } console.log( "El producto se ha guardado en el archivo satisfactoriamente" ); return obj.id; } catch (error) { console.error("No se ha podido guardar"); } } async getById(id) { let type = await this.fileInJSON(); let product = type.find((product) => product.id == id); return console.log(product); } async getAll() { let type = await this.fileInJSON(); return console.log(type); } async deleteAll() { let item = []; this.products = item; this.fileSaving(item); } async deleteById(number) { let type = await this.fileInJSON(); let item = type.find((item) => item.id === number); let index = type.indexOf(item); type.splice(index, 1); this.fileSaving(type); } } module.exports = Contenedor;

[servidoraplicaciones.js]

 const express = require("express"); const app = express(); const PORT = 3000; const Contenedor = require("./app.js"); app.get("/", (req, res) => { res.send("Ingresa a la ruta /productos para ver los productos :D"); }); app.get("/productos", (req, res) => { res.send(Contenedor.getAll()); }); const servidor = app .listen(PORT, () => { console.log(`Servidor corriendo en el puerto ${PORT}`); }) .on("error", (error) => console.error("FALLASTE" + error));
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Tal vez debería crear una instancia de un objeto Contenedor utilizando el operador nuevo.

 const express = require("express"); const app = express(); const PORT = 3000; const Contenedor = require("./app.js"); const myContainer = new Contenedor("example.txt"); app.get("/", (req, res) => { res.send("Ingresa a la ruta /productos para ver los productos :D"); }); app.get("/productos", (req, res) => { res.send(myContainer.getAll()); }); const servidor = app .listen(PORT, () => { console.log(`Servidor corriendo en el puerto ${PORT}`); }) .on("error", (error) => console.error("FALLASTE" + error));
about 4 years ago · Juan Pablo Isaza Report

0

primero debe acceder a esa clase.

 app.get("/productos", (req, res) => { const test_class = new Contenedor() res.send(test_class.getAll()); });
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!