aionix 1.0.3 → 1.0.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server/app.js +18 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aionix",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Offline Developer Toolkit - Learning, Tools & Productivity",
5
5
  "main": "server/app.js",
6
6
  "bin": {
package/server/app.js CHANGED
@@ -3,6 +3,7 @@ const express = require('express');
3
3
  const path = require('path');
4
4
  const open = require('open');
5
5
  const chalk = require('chalk');
6
+ const net = require('net');
6
7
 
7
8
  const snippetsRoute = require('./routes/snippets');
8
9
  const quizRoute = require('./routes/quiz');
@@ -10,26 +11,36 @@ const habitsRoute = require('./routes/habits');
10
11
  const docsRoute = require('./routes/docs');
11
12
 
12
13
  const app = express();
13
- const PORT = 3000;
14
14
 
15
15
  app.use(express.json());
16
16
  app.use(express.static(path.join(__dirname, '../client')));
17
17
 
18
- // Routes
19
18
  app.use('/api/snippets', snippetsRoute);
20
19
  app.use('/api/quiz', quizRoute);
21
20
  app.use('/api/habits', habitsRoute);
22
21
  app.use('/api/docs', docsRoute);
23
22
 
24
- // Main page
25
23
  app.get('/', (req, res) => {
26
24
  res.sendFile(path.join(__dirname, '../client/index.html'));
27
25
  });
28
26
 
29
- app.listen(PORT, async () => {
30
- console.log(chalk.green(`✅ Server running at http://localhost:${PORT}`));
31
- console.log(chalk.yellow('Opening browser...'));
32
- await open(`http://localhost:${PORT}`);
27
+ // Auto find available port
28
+ function getPort(start) {
29
+ return new Promise((resolve) => {
30
+ const server = net.createServer();
31
+ server.listen(start, () => {
32
+ server.close(() => resolve(start));
33
+ });
34
+ server.on('error', () => resolve(getPort(start + 1)));
35
+ });
36
+ }
37
+
38
+ getPort(3000).then(PORT => {
39
+ app.listen(PORT, async () => {
40
+ console.log(chalk.green(`✅ Server running at http://localhost:${PORT}`));
41
+ console.log(chalk.yellow('Opening browser...'));
42
+ await open(`http://localhost:${PORT}`);
43
+ });
33
44
  });
34
45
 
35
46
  module.exports = app;