-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (31 loc) · 1.11 KB
/
server.js
File metadata and controls
41 lines (31 loc) · 1.11 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
const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const cors = require('cors');
const server = express();
require('dotenv').config();
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
server.use(bodyParser.urlencoded({ extended: true }));
server.use(bodyParser.json());
const corsOptions = {
origin: 'http://localhost:3000',
methods: "GET, HEAD, PUT, PATCH, POST, DELETE",
credentials: true,
preflightContinue: false,
optionsSuccessStatus: 204
};
server.use(cors(corsOptions));
const routes = require('./api/routes');
routes(server);
// Priority serve any static files.
server.use(express.static(path.resolve(__dirname, './client/build')));
// All requests return the React app, so it can handle routing.
server.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../client/build', 'index.html'));
});
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log(`Server up and running on ${port}`);
});