-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (66 loc) · 2.59 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// server.js
// set up ======================================================================
// get all the tools we need
var express = require('express');
var app = express();
var port = process.env.PORT || 3000;
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
const http = require('http');
var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var path = require('path');
var cron = require('cron');
var configDB = require('./config/database.js');
mongoose.Promise = global.Promise;
// configuration ===============================================================
mongoose.connect(configDB.url , function(err){
if(err) console.log('Unable to connect to DB ' + err);
else console.log('Connection to DB successful')
}); // connect to our database
mongoose.set('debug', true);
require('./config/passport')(passport); // pass passport for configuration
// set up our express application
app.use(morgan('dev')); // log every request to the console
app.use(cookieParser()); // read cookies (needed for auth)
app.use(bodyParser.json()); // get information from html forms
app.use(bodyParser.urlencoded({ extended: true }));
// required for passport
app.use(session({
secret: 'bnb2018underdevelopment', // session secret
// cookie: {domain : '/'},
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
// routes ======================================================================
require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport
//Tasks
// require('./app/tasks')(cron);
// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist')));
// Get our API routes
// const api = require( './server/routes/api');
// Set our api routes
// app.use('/api', api);
// Catch all other routes and return the index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
// launch ======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);
// app.set('port', port);
/**
* Create HTTP server.
*/
// const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
// server.listen(port, () => console.log(`API running on localhost:${port}`));