-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (50 loc) · 1.61 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
const express = require('express');
const fileUpload = require('express-fileupload');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
const path = require('path');
const AWS = require('aws-sdk');
AWS.config.apiVersions = {
ses: '2010-12-01',
s3: '2006-03-01',
};
AWS.config.region = 'us-west-2';
// MongoDB URI
const mongoURI = require('./config/keys').mongoURI;
// Connect to MongoDB
mongoose
.connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB successfully connected at ' + mongoURI))
.catch((err) => console.log(err));
// Add the validation error plugin to make the model validation
// errors look nice.
mongoose.plugin(require('./models/plugins/validationError'));
// Add models to mongoose
mongoose.model('User', require('./models/User'));
const app = express();
// Bodyparser middleware
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(bodyParser.json());
// Passport middleware
app.use(passport.initialize({}));
// Passport config
require('./config/passport')(passport);
app.use(fileUpload());
// Routes
app.use('/api/users', require('./routes/api/users/users'));
// Serve static assets if in production
if (process.env.NODE_ENV === 'production') {
//Set static folder
app.use(express.static('client/build'));
app.get('*', (_, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server up and running on port ${port} !`));
module.exports = app;