I have the below code in my app.ts file. How can I export the app instance so that I can use it with superagent lib for my tests?
const port = 8080;
const host = "0.0.0.0";
const init = async () => {
const app = express();
const middlewareA = await middlewareA();
app.use([middlewareA]);
app.use(routes);
app.listen(port, host, () => {
console.log(Working server on ${host}:${port});
});
};
init();
You cannot export the
appasynchronously, but you can export the asynchronousinitfunction and let it resolve to theapponce it has started listening:followed by
module.exports = init;for Common JS modules or byexport default init;for ES modules.In order to use the
init()function, you can then either import it and await its result in an ES module, or you can defer theinituntil the first request comes in that needs it. Both options are explained here.