-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
dummy-server.js
46 lines (40 loc) · 1.05 KB
/
dummy-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const express = require("express");
const app = express();
/**
* - 3000 port is for the dummy server to run on
* - 5432 is a proxy
*/
let config = {
defaultPort: 3000,
};
/**
* This is just a dummy `test-server` to ensure sniffing is working properly
*
* Access this API via http://localhost:5432 through postman
*/
app.get("*", async (_req, res) => {
try {
res.json({ messaage: "Checkout Sniffer, API logs would be made" });
} catch (error) {
console.error(error.message);
res.json({ messaage: "API call failed" });
}
});
init = (port) => {
app.listen(port, () => {
console.log(`Dummy 'test-server' Server is running on port ${port}`);
});
};
// Attempt to start the server on the default port
init(config.defaultPort);
app.on("error", (err) => {
if (err.code === "EADDRINUSE") {
const alternativePort = defaultPort + 1;
console.log(
`Port ${defaultPort} is already in use. Trying port ${alternativePort} to initialize dummy test-server...`,
);
init(alternativePort);
} else {
console.error(err);
}
});