I'm using the express framework in NodeJS with typescript in the backend. I have a very easy architecture in my index file:
index.html
import express = require("express");
import mongoClient = require("mongodb");
import apiRoutes from "./routes";
import { MONGO_CONNECTION } from "./config/mongo_config";
const app = express();
mongoClient.MongoClient.connect(MONGO_CONNECTION, { }, (err: any, mclient: any) => {
if (err) throw err;
const mongodb = mclient.db('test');
app.use('/api', isOnline, apiRoutes);
app.listen(80, () => console.log('API running on port 80'));
});
The express routes are separated in an other file (in my version it is separated in multiple file, just to keep it simple), here just an example:
routes/index.ts
import express = require("express");
import { Router } from "express";
const router = Router({mergeParams: true});
router.get('/example', (req: express.Request, res: express.Response) => {
res.json('Hello World');
});
export default router;
I don't want to use mongoose. So is there any way to pass the DB connection to another file without connecting again?