server-up-ndot 1.1.8 → 1.2.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "server-up-ndot",
3
- "version": "1.1.8",
3
+ "version": "1.2.0-alpha.2",
4
4
  "description": "server toolkit",
5
5
  "main": "index.js",
6
6
  "bin": {
package/readme.md CHANGED
@@ -1,3 +1,12 @@
1
+ ⚠️ alpha.1 version.
2
+ May contain bugs. Possibly many.
3
+
4
+ If you find one, don't panic.
5
+ If you panic, your computer might also kernel panic!
6
+
7
+ At the same time, my nickname "segfaultandsegmentationfault"
8
+ may be activated.
9
+
1
10
  # server-up-ndot
2
11
 
3
12
  Simple server generator and auto library install for Node.js.
@@ -25,6 +34,9 @@ npx server-up-ndot create myserver
25
34
 
26
35
  ## Patch Notes
27
36
 
37
+ ### 1.2.0-alpha.1
38
+ - Prevent port overlap(alhpa)
39
+
28
40
  ### 1.1.8
29
41
  - typo correction
30
42
 
@@ -1,9 +1,10 @@
1
1
  const express = require('express');
2
2
  const cors = require('cors');
3
+ const net = require('net');
3
4
  require('dotenv').config();
4
5
 
5
6
  const app = express();
6
- const PORT = process.env.PORT || 3000;
7
+ const startPort = process.env.PORT || 3000;
7
8
 
8
9
  app.use(cors());
9
10
  app.use(express.json());
@@ -18,7 +19,39 @@ app.post('/api', (req, res) => {
18
19
  res.json({ received: req.body });
19
20
  });
20
21
 
21
- // server start
22
- app.listen(PORT, () => {
23
- console.log(`Server running on http://localhost:${PORT}`);
24
- });
22
+ function checkPort(port) {
23
+ return new Promise((resolve) => {
24
+ const server = net.createServer();
25
+
26
+ server.once('error', () => resolve(false));
27
+
28
+ server.once('listening', () => {
29
+ server.close();
30
+ resolve(true);
31
+ });
32
+
33
+ server.listen(port);
34
+ });
35
+ }
36
+
37
+ async function findPort(start = 3000) {
38
+ let port = start;
39
+
40
+ while (!(await checkPort(port))) {
41
+ console.log(`⚠️ Port ${port} in use`);
42
+ port++;
43
+ console.log("⚠️trying port ${port}")
44
+ }
45
+
46
+ return port;
47
+ }
48
+
49
+ async function start() {
50
+ const PORT = await findPort(startPort);
51
+
52
+ app.listen(PORT, () => {
53
+ console.log(`🚀 Server running on http://localhost:${PORT}`);
54
+ });
55
+ }
56
+
57
+ start();