Currently, I am developing an Express application with multiple routes. To ensure modularity, each route will have its own file stored in a dedicated routes folder.
One challenge I encountered is sharing a common value across all routes. Specifically, I need to pass a certain value from my app.js file to a route.js file.
For instance:
//app.js file
const express = require("express");
const app = express();
const port = 3000;
const url = "https://example.com/v1"; // How can I share this value with the test route below?
const test = require("./routes/test")
app.use(express.json());
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
// routes/test.js
const express = require("express");
const router = express.Router();
//Middleware for requests
router.use((req, res, next) => {
console.log("Time:", Date.now());
next();
});
router.post("/", (req, res) => {
console.log(url)
});