Skip to content

Commit 1c0a0fa

Browse files
author
vedantoncloud
committed
fix: export hello() and avoid starting server during tests
1 parent eac09f8 commit 1c0a0fa

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

app.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
const express = require("express");
1+
// app.js
2+
const express = require("express");
23
const app = express();
34

5+
// keep the old test string for the unit test
6+
function hello() {
7+
return "Hello from MyApp - dev";
8+
}
9+
10+
// public route shows CI/CD message
411
app.get("/", (req, res) => {
512
res.send("Hello from MyApp - CI/CD Working!");
613
});
714

8-
const PORT = process.env.PORT || 3000;
9-
app.listen(PORT, () => {
10-
console.log(`Server running on port ${PORT}`);
11-
});
15+
// export the Express app and the hello() function for tests
16+
module.exports = { app, hello };
1217

13-
module.exports = app;
18+
// Only start the server when this file is run directly (node app.js),
19+
// not when imported by tests (require).
20+
if (require.main === module) {
21+
const PORT = process.env.PORT || 3000;
22+
app.listen(PORT, () => {
23+
// only log when actually starting the server
24+
console.log(`Server running on port ${PORT}`);
25+
});
26+
}

0 commit comments

Comments
 (0)