-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
103 lines (79 loc) · 2.69 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const path = require('path');
const cloudinary = require('cloudinary');
const app = express();
// Body parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// DB Config
const db = require('./config/keys').mongoURI;
// Cloudinary config
const cloud_name = require('./config/keys').cloud_name;
const cloud_api_key = require('./config/keys').cloud_api_key;
const cloud_api_secret = require('./config/keys').cloud_api_secret;
// Prerender config
const prerender_token = require('./config/keys').prerender_token;
// Contentful config see articles js
// Connect to MongoDB
mongoose
.connect(db, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
})
.then(() =>
console.log(`MongoDB Connected to database: ${db.substring(db.length - 7)}`)
)
.catch(err => console.log(err));
// Routes to use
const users = require('./routes/api/users');
const profile = require('./routes/api/profile');
const posts = require('./routes/api/posts');
const product = require('./routes/api/product');
const order = require('./routes/api/order');
const resume = require('./routes/api/resume');
const articles = require('./routes/api/articles');
const tasks = require('./routes/api/tasks');
const gcse = require('./routes/api/gcse');
const portfolio = require('./routes/api/portfolio');
// Passport middleware
app.use(passport.initialize());
// Middleware for Prerender.io
app.use(require('prerender-node').set('prerenderToken', prerender_token));
// Passport Config
require('./config/passport')(passport);
// Cloudinary config
cloudinary.config({
cloud_name: cloud_name,
api_key: cloud_api_key,
api_secret: cloud_api_secret
});
// For contenful see artices.js
// for env variables
require('dotenv').config();
// Use Routes
app.use('/api/users', users);
app.use('/api/profile', profile);
app.use('/api/posts', posts);
app.use('/api/product', product);
app.use('/api/order', order);
app.use('/api/resume', resume);
app.use('/api/articles', articles);
app.use('/api/tasks', tasks);
app.use('/api/gcse', gcse);
app.use('/api/portfolio', portfolio);
// Server static assets if in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
console.log('production build');
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on port ${port}`));