-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (56 loc) · 2.23 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
// set up ======================================================================
var express = require('express');
var app = express();
const path = require('path');
require('dotenv').load();
const favicon = require('serve-favicon');
var mongoose = require('mongoose');
var cors = require('cors');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var https = require('https');
var configDB = require('./app/config/database.js');
const User = require('./app/models/user');
const session = require('express-session');
const passport = require('passport');
const user = require('./app/utils/passport-serialize');
const MongoStore = require('connect-mongo')(session);
// configuration ===============================================================
mongoose.connect(configDB.url, configDB.options); // connect to db
mongoose.Promise = global.Promise;
require('./app/config/passport')(passport); // pass passport for configuration
// Basic middleware for all Express requests
app.use(favicon(path.join(__dirname, '/client/public/favicon.ico'))); // serve favicon
app.use(bodyParser.urlencoded({ extended: true })); // Parses urlencoded bodies
app.use(bodyParser.json()); // Send JSON responses
app.use(morgan('dev')); // Log requests to API using morgan
// Enable CORS from client side
app.use(cors());
// app.use(session({
// store: new MongoStore({
// mongooseConnection: mongoose.connection
// }),
// secret: process.env.JWT_SECRET,
// resave: false,
// saveUninitialized: true
// }));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(user.serialize);
passport.deserializeUser(user.deserialize);
// set static path
app.use(express.static(path.join(__dirname, '/client/build/')));
app.get('/', (req, res) => {
console.log('root route, serving client');
res.status(200)
.sendFile(path.join(__dirname, '../client/build/index.html'));
});
// routes ======================================================================
const router = require('./router');
router(app);
// launch ======================================================================
var port = process.env.PORT || 3001;
app.listen(port, function () {
console.log('Node.js listening on port ' + port + '...');
});