55
66const express = require ( 'express' )
77const path = require ( 'path' )
8- const dns = require ( 'dns' )
98const os = require ( 'os' )
109const mongoose = require ( 'mongoose' )
1110const https = require ( './middleware/https.middleware' )
11+ const startWSS = require ( './socket/wss' )
1212require ( 'dotenv' ) . config ( )
1313
14- /**
15- * подключение переменных среды
16- */
14+ //подключение переменных среды
1715const devMode = process . env . NODE_ENV === "dev"
1816const PORT = process . env . PORT || 5000
1917const mongoUri = process . env . mongoUri
2018const httpsRedirect = process . env . httpsRedirect || false
19+ const WS_PORT = process . env . WS_PORT || 3030
2120
2221const app = express ( )
2322
2423app . use ( express . json ( { extended : true } ) )
2524
25+ app . get ( '/getIp' , ( req , res ) => {
26+ const ip = getIp ( )
27+ res . status ( 200 ) . json ( { ip } )
28+ } )
29+
2630/**
2731 * подключение роутов
2832 */
2933app . use ( '/api/auth' , require ( './routes/auth.routes' ) )
3034app . use ( '/api/notes' , require ( './routes/notes.routes' ) )
3135
36+
3237if ( httpsRedirect ) app . use ( https )
3338
3439/**
@@ -52,6 +57,7 @@ if (!devMode) {
5257async function start ( ) {
5358 try {
5459 connectMongo ( mongoUri )
60+ startWSS ( WS_PORT )
5561 app . listen ( PORT , logServerStart )
5662 } catch ( e ) {
5763 console . log ( 'Server Error' , e . message )
@@ -81,11 +87,18 @@ async function connectMongo(mongoUri) {
8187 * Вывод информации о сервере
8288 */
8389function logServerStart ( ) {
84- dns . lookup ( os . hostname ( ) , ( err , address ) => {
85- const [ logName , sBef , sAft ] = devMode ? [ 'Express server' , ' ' , ':' ] : [ 'React Notes App' , '-' , '' ]
86- console . log ( `\n${ logName } has been started` )
87- console . log ( `${ sBef } Local${ sAft } http://localhost:${ PORT } ` )
88- console . log ( `${ sBef } On Your Network${ sAft } http://${ address } :${ PORT } ` )
89- if ( err ) console . log ( err )
90- } )
90+ const [ logName , sBef , sAft ] = devMode ? [ 'Express server' , ' ' , ':' ] : [ 'React Notes App' , '-' , '' ]
91+ console . log ( `\n${ logName } has been started` )
92+ console . log ( `${ sBef } Local${ sAft } http://localhost:${ PORT } ` )
93+ console . log ( `${ sBef } On Your Network${ sAft } http://${ getIp ( ) } :${ PORT } ` , '\n' )
94+ }
95+
96+ /**
97+ * Получение ip сервера
98+ */
99+ function getIp ( ) {
100+ for ( let key in os . networkInterfaces ( ) ) {
101+ const addr = os . networkInterfaces ( ) [ key ] [ 1 ] . address
102+ if ( addr != undefined ) return addr
103+ }
91104}
0 commit comments