-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
68 lines (53 loc) · 1.86 KB
/
app.js
File metadata and controls
68 lines (53 loc) · 1.86 KB
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
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
const session = require('express-session');
const passport = require('passport');
require('dotenv').config();
// Import Mongoose Models
require('./models');
require('./config/passport')(passport);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(morgan('combined'));
// Express Session
app.use(session({
secret: 'yapper secret',
resave: true,
saveUninitialized: true,
//cookie: { secure: true }
}));
app.use(passport.initialize());
app.use(passport.session());
// enable CORS
app.use(cors({credentials: true, origin: true}));
app.options('*', cors({credentials: true, origin: true}));
// serve up static assets
app.use('/static', express.static('public'));
// serve up static React app for production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
// Configure Mongoose
const mongoURI = process.env.NODE_ENV === 'test' ? process.env.mongo_cluster_connection_test : process.env.mongo_cluster_connection;
mongoose.connect(mongoURI || 'mongodb://localhost/yapper', { useUnifiedTopology: true, useNewUrlParser: true }).then(
() => console.log(`MongoDB connected for ${process.env.NODE_ENV}.`)
).catch(err => console.error(err));
// mongoose.set('debug', true);
// setup routes
app.use(require('./routes'));
// 404 route
app.use((req, res, next) => {
const err = new Error(`The requested URL ${req.originalUrl} was not found on this server. That's all we know.`);
err.status = 404;
next(err);
});
// error handler
app.use((err, req, res) => {
err.status = err.status || 500;
res.status(err.status).redirect(`${process.env.webUrl}?404=true`);
});
module.exports = { app, mongoose };