E
E
EvgenyJS2021-07-28 00:57:22
Node.js
EvgenyJS, 2021-07-28 00:57:22

How to use the base in modules?

I have a module in which the database is connected, I need to use it in all Express routers.
I could create a new connection, but I would like to somehow pass the class of work with the base through the context.
Please help me with advice to solve this problem.

app.use("/method", require("./test"));

const { Router } = require("express");
const router = Router();

router.get("/register", async (req, res) => {
  const user = await db.query("SELECT * FROM `users`");
  res.status(200).json({ result: { user } });
});

module.exports = router;


Here somehow it is necessary to transfer the class

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question