-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (74 loc) · 2.29 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
try {
require('express');
runStart();
} catch(err) {
firstStart();
}
async function firstStart() {
const fs = require("fs");
const path = require("path");
const { exec } = require("child_process");
console.log("Вы запустили сервер в первый раз. Подождите немного, пока он настроится.");
exec("npm i", (err, stdout, stderr) => {
if (err) {
console.log(err);
}
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.log(stderr);
}
fs.writeFile("server.bat", `::door detect node.js app by @Jeimip
cd ${path.resolve()}
forever start --uid nodeServer --append .\\index.js`,
err => {
if (err) {
return console.log(err)
}
}
);
exec("npm install forever -g", (err, stdout, stderr) => {
if (err) {
console.log(err);
}
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.log(stderr);
}
console.log("Сервер настроен!");
process.exit(22);
});
})
}
async function runStart() {
const express = require('express');
const { networkInterfaces } = require('os');
const fs = require("fs");
const ApiController = require('./routes/api.controller');
const app = express();
app.use(express.json());
app.use("/api", ApiController);
fs.readFile("server.bat", async (err) => {
if (err) {
await firstStart();
}
});
const nets = await networkInterfaces();
let bindIp;
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
if (net.address.includes("192.168")) { //получаем локальный IP устройства
bindIp = net.address;
}
}
}
fs.writeFile("data.txt", `Ссылка для ввода в приложение: ${bindIp}`, async (err) => {
if (err) {
await firstStart();
}
});
app.listen(80, bindIp, () => {console.log(`Server started on ${bindIp}:80`)}); //запускаем сервер
}