-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
51 lines (43 loc) · 1.56 KB
/
server.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
const express = require( "express" ),
bodyParser = require( "body-parser" ),
cors = require( "cors" ),
path = require( 'path' ),
logger = require( 'morgan' ),
mongoose = require( 'mongoose' ),
config = require( './app/config/main' ),
{ Server } = require( "socket.io" ),
socketEvents = require( './app/socket/socketEvents' ),
app = express(),
swaggerUi = require( 'swagger-ui-express' ),
swaggerDocument = require( './swagger.json' );
// Connect to the database
mongoose.connect( config.database ).then( r =>
{
console.log( "Mongo Db conneted!" );
} );
let corsOptions = {
origin : "http://localhost:" + config.port
};
app.use( cors( corsOptions ) );
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded( { extended : true } ) );
app.use( express.json() );
app.use( express.static( path.join( __dirname, 'app/public' ) ) );
app.use( logger( 'dev' ) );
app.use( '/api-docs', swaggerUi.serve, swaggerUi.setup( swaggerDocument ) );
//Enables CORS from client-side
app.use( function ( req, res, next )
{
res.header( "Access-Control-Allow-Origin", "*" );
res.header( 'Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS' );
res.header( "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, Access-Control-Allow-Credentials" );
res.header( "Access-Control-Allow-Credentials", "true" );
next();
} );
require( "./app/routes/init.routes.js" )( app );
const server = app.listen( config.port, () =>
{
console.log( `Server is running on port ${config.port}.` );
} );
const io = new Server( server );
socketEvents( io );