-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
113 lines (98 loc) · 2.92 KB
/
app.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* @file app.js
* @breif main file
*/
const express = require('express')
const path = require('path')
const os = require('os')
const mongoose = require('mongoose')
const https = require('./middleware/https.middleware')
const startWSS = require('./socket/wss')
require('dotenv').config()
//подключение переменных среды
const devMode = process.env.NODE_ENV === "dev"
const PORT = process.env.PORT || 5000
const mongoUri = process.env.mongoUri
const httpsRedirect = process.env.httpsRedirect || false
const WS_PORT = process.env.WS_PORT || 3030
const app = express()
app.use(express.json({ extended: true, limit: "1mb" }))
app.get('/getSocketAddress', (req, res) => {
getIp()
.then((ip) => {
const socketAddress = ((req.protocol === 'https' || httpsRedirect) ? "wss" : "ws") + "://" + (ip || "localhost") + ":" + WS_PORT
res.status(200).json({ socketAddress })
})
.catch(() => res.status(500))
})
/**
* подключение роутов
*/
app.use('/api/auth', require('./routes/auth.routes'))
app.use('/api/notes', require('./routes/notes.routes'))
app.use('/api/media', require('./routes/media.routes'))
if (httpsRedirect) app.use(https)
/**
* подключение статической библиотеки клиента
*/
if (!devMode) {
const clientPath = '../client/build'
app.use('/', express.static(path.join(__dirname, clientPath)))
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, clientPath, 'index.html'))
})
} else {
app.get('*', (req, res) => {
res.send("[Backend] NODE_ENV is 'dev'")
})
}
/**
* запуск сервера
*/
async function start() {
try {
connectMongo(mongoUri)
startWSS(WS_PORT)
app.listen(PORT, logServerStart)
} catch (e) {
console.log('Server Error', e.message)
process.exit(1)
}
}
start()
/**
* подключение к MongoDb
* @param {*} mongoUri
*/
async function connectMongo(mongoUri) {
if (mongoUri) {
await mongoose.connect(mongoUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true
})
} else {
console.log("\n!!!NO MONGO URI!!!")
}
}
/**
* Вывод информации о сервере
*/
async function logServerStart() {
const [logName, sBef, sAft] = devMode ? ['Express server', ' ', ':'] : ['React Notes App', '-', '']
const ip = await getIp()
console.log(`\n${logName} has been started`)
console.log(`${sBef} Local${sAft} http://localhost:${PORT}`)
console.log(`${sBef} On Your Network${sAft} http://${ip}:${PORT}\n`)
}
/**
* Получение ip сервера
*/
function getIp() {
return new Promise((res, rej) => {
require('dns').lookup(os.hostname(), (err, addr) => {
addr ? res(addr) : rej()
err && console.log(err)
})
})
}