The mount event fires on a sub-application the moment it's mounted onto a parent app. The
parent app instance is passed into the callback.
Syntax
app.on("mount", callback(parent))
Example
const express = require("express");
const app = express();
const reportsApp = express();
const PORT = 4900;
reportsApp.on("mount", function (parent) {
console.log("Reports sub-app mounted");
});
reportsApp.get("/", (req, res) => {
res.send("Reports Home");
});
app.use("/reports", reportsApp);
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
Starting the server logs Reports sub-app mounted immediately, before any request even
arrives.
Use cases
- Running setup logic exactly when a sub-app gets attached.
- Confirming that multiple sub-apps mounted correctly at startup.
- Debugging composition of larger, modular Express applications.