Browse Source

Gestion port déjà occupé

Arthur Brandao 5 years ago
parent
commit
c894441348
5 changed files with 73 additions and 34 deletions
  1. 2 1
      node/config.json
  2. 35 4
      node/main.js
  3. 30 29
      node/package.json
  4. 2 0
      node/readme.md
  5. 4 0
      node/src/config.js

+ 2 - 1
node/config.json

@@ -1,4 +1,5 @@
 {
   "storage": "database",
-  "auth": true
+  "auth": true,
+  "findPort": true
 }

+ 35 - 4
node/main.js

@@ -33,6 +33,7 @@ const config = require('./src/config');
 
 // Bibliotheques
 const express = require('express');
+const portfinder = require('portfinder');
 
 // Creation variable globale
 global.app = express();
@@ -49,7 +50,37 @@ global.sqlVerbose = argv.sql >= 1;
 app.use(express.json());
 require('./src/route');
 
-// Lancement serveur
-app.listen(argv.port, () => {
-    console.info(`Server starting on port ${argv.port} (http://localhost:${argv.port})`);
-});
+// Lancement serveur sur le port demandé
+portfinder.basePort = argv.port;
+portfinder.highestPort = argv.port;
+portfinder.getPortPromise()
+    .then((port) => {
+        app.listen(argv.port, () => {
+            console.info(`Server starting on port ${port} (http://localhost:${port})`);
+        });
+    })
+    .catch((err) => {
+        if(err.toString().includes('Error: No open ports found') && config.findPort) {
+            console.info(`Port ${argv.port} not available, search for a new available port`);
+        } else {
+            console.error(err.toString());
+            console.info('Unable to start the server, end of execution');
+            process.exit();
+        }
+    });
+
+// Recherche d'un port ouvert
+portfinder.basePort = 8000;
+portfinder.highestPort = 65535;
+portfinder.getPortPromise()
+    .then((port) => {
+        app.listen(port, () => {
+            console.info(`New available port found: ${port}`);
+            console.info(`Server starting on port ${port} (http://localhost:${port})`);
+        });
+    })
+    .catch((err) => {
+        console.err(err);
+        console.info('Unable to start the server, end of execution');
+        process.exit();
+    });

+ 30 - 29
node/package.json

@@ -1,31 +1,32 @@
 {
-    "name": "loquicompta_node_server",
-    "version": "0.0.1",
-    "description": "Server de stockage node js",
-    "homepage": "https://github.com/Loquicom/",
-    "author": {
-        "name": "Loquicom",
-        "email": "contact@loquicom.fr"
-    },
-    "keywords": [
-        "node",
-        "nodejs",
-        "server",
-        "loquicompta"
-    ],
-    "main": "main.js",
-    "private": true,
-    "scripts": {
-        "start": "node main.js",
-        "dev": "node main.js dev"
-    },
-    "engines": {
-        "node": "=10"
-    },
-    "dependencies": {
-        "bcryptjs": "^2.4.3",
-        "express": "^4.17.1",
-        "sqlite3": "^4.0.9",
-        "yargs": "^13.3.0"
-    }
+  "name": "loquicompta_node_server",
+  "version": "0.0.1",
+  "description": "Server de stockage node js",
+  "homepage": "https://github.com/Loquicom/",
+  "author": {
+    "name": "Loquicom",
+    "email": "contact@loquicom.fr"
+  },
+  "keywords": [
+    "node",
+    "nodejs",
+    "server",
+    "loquicompta"
+  ],
+  "main": "main.js",
+  "private": true,
+  "scripts": {
+    "start": "node main.js",
+    "dev": "node main.js dev"
+  },
+  "engines": {
+    "node": "=10"
+  },
+  "dependencies": {
+    "bcryptjs": "^2.4.3",
+    "express": "^4.17.1",
+    "sqlite3": "^4.0.9",
+    "yargs": "^13.3.0",
+    "portfinder": "^1.0.24"
+  }
 }

+ 2 - 0
node/readme.md

@@ -1,3 +1,5 @@
 # Implementation Node JS de Loquicompta Server
 
 Pour stocker les données 2 façons : Soit dans le champ data de la base directement, soit dans un fichier dont le nom du chemin est dans le champ data de la base
+
+FindPort dans config.json indique si le serveur va cherhcer un nouveau port libre si le port demandé est déjà utilisé

+ 4 - 0
node/src/config.js

@@ -17,6 +17,8 @@ if (config.storage === undefined) {
     throw 'Storage undefined in the config file';
 } else if (config.auth === undefined) {
     throw 'Auth undefined in the config file';
+} else if (config.findPort === undefined) {
+    throw 'findPort undefined in the config file';
 }
 
 //Verification valeur
@@ -24,6 +26,8 @@ if (config.storage !== 'database' && config.storage !== 'file') {
     throw 'Bad value for storage: database or file expected';
 } else if (typeof config.auth !== 'boolean') {
     throw 'Bad value for auth: boolean expected';
+} else if (typeof config.findPort !== 'boolean') {
+    throw 'Bad value for findPort: boolean expected';
 }
 
 module.exports = config;