-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
68 lines (58 loc) · 1.91 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
// Dependencies
const express = require('express');
const passport = require('./config/passport');
const session = require('express-session');
const MemoryStore = require('memorystore')(session);
const logger = require('morgan');
const mongoose = require('mongoose');
const routes = require('./routes');
const flash = require('connect-flash');
// Set up the express app
const app = express();
const PORT = process.env.PORT || 3001;
// Requiring our models folder for syncing
const db = require('./models');
// Define middleware that handles data parsing
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(flash());
// We need to use sessions to keep track of our user's login status
app.use(
session({
cookie: { maxAge: 86400000 },
store: new MemoryStore({
checkPeriod: 86400000, // prune expired entries every 24h
}),
resave: true,
saveUninitialized: true,
secret: 'keyboard cat',
}),
);
app.use(passport.initialize());
app.use(passport.session());
app.use(logger('dev'));
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
// Add routes, both API and view
// app.use('/meals', require('./routes/api/mealRoutes'));
// require('./routes/api/mealRoutes')(app);
app.use(routes);
// Connect to the Mongo DB
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/Brotein', {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false
});
// test that we connect successfully or if a connection error occurs
const DB = mongoose.connection;
DB.on('error', console.error.bind(console, 'connection error:'));
DB.once('open', function () {
console.log('Successfully Connected!!');
});
// Start the server and sync Sequelize models
app.listen(PORT, function() {
console.log(`🌎 ==> API Server now listening on PORT ${PORT}!`);
});