This repository has been archived by the owner on Dec 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathserver.conf.js
134 lines (99 loc) · 3.42 KB
/
server.conf.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// ```
// server.conf.js
// (c) 2016 David Newman
// david.r.niciforovic@gmail.com
// server.conf.js may be freely distributed under the MIT license
// ```
// *server.conf.js*
// This is the file where we will:
// - Configure our application
// - Connect to our database
// - Create our Mongoose models
// - Define routes for our RESTful API
// - Define routes for our frontend Angular application
// - Set the app to listen on a port so we can view it in our browser
// # Node Env Variables
// Load Node environment variable configuration file
import {validateEnvVariables} from './config/env.conf.js';
// Set up appropriate environment variables if necessary
validateEnvVariables();
// # Modules
// Load Express
import express from 'express';
// Load Socket.io
import socketio from 'socket.io';
// Load Node http module
import http from 'http';
// Create our app with Express
let app = express();
// Create a Node server for our Express app
let server = http.createServer(app);
// Integrate Socket.io
let io = socketio.listen(server);
// Load Mongoose for MongoDB interactions
import mongoose from 'mongoose';
// Log requests to the console (Express 4)
import morgan from 'morgan';
// Pull information from HTML POST (express 4)
import bodyParser from 'body-parser';
// Simulate DELETE and PUT (Express 4)
import methodOverride from 'method-override';
// PassportJS
import passport from 'passport';
import cookieParser from 'cookie-parser';
import session from 'express-session';
// # Configuration
// Load Socket.io server functionality
import base from './sockets/base';
base(io);
// Set the port for this app
let port = process.env.PORT || 8080;
// Load Mongoose config file for connecting to MongoDB instance
import mongooseConf from './config/mongoose.conf.js';
// Pass Mongoose configuration Mongoose instance
mongooseConf(mongoose);
// Import PassportJS configuration
import passportConf from './config/passport.conf.js';
// Pass Passport configuration our PassportJS instance
passportConf(passport);
if (process.env.NODE_ENV === 'development' ||
process.env.NODE_ENV === 'test')
// Log every request to the console
app.use(morgan('dev'));
// Read cookies (needed for authentication)
app.use(cookieParser());
// ## Get all data/stuff of the body (POST) parameters
// Parse application/json
app.use(bodyParser.json());
// Parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
// Parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// Override with the X-HTTP-Method-Override header in the request. Simulate DELETE/PUT
app.use(methodOverride('X-HTTP-Method-Override'));
// Set the static files location /public/img will be /img for users
app.use(express.static(__dirname + '/dist'));
// ## Passport JS
// Session secret
app.use(session({
secret : process.env.SESSION_SECRET,
resave : true,
saveUninitialized : true
}));
app.use(passport.initialize());
// Persistent login sessions
app.use(passport.session());
// ## Routes
// Get an instance of the express Router
let router = express.Router();
// Load our application API routes
// Pass in our express and express router instances
import routes from './app/routes';
// Pass in instances of the express app, router, and passport
routes(app, router, passport);
// ### Ignition Phase
server.listen(port);
// Shoutout to the user
console.log(`Wizardry is afoot on port ${port}`);
// Expose app
export {app};