-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (42 loc) · 1.4 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
const express = require('express');
const app = express();
const parh = require('path');
const fs = require('fs');
app.use(express.json());
const cors = require('cors');
app.use(
cors({
origin: 'http://localhost:3000' || 'http://blog-react-slate.herokuapp.com',
credentials: true
})
);
const mongoose = require('mongoose');
//Husk den nu hvis du skal uploade filer (-_-)
const fileUpload = require('express-fileupload');
app.use(fileUpload());
//Husk cookieParser ellers virker withCredentials ikke
const cookieParser = require('cookie-parser');
app.use(cookieParser());
//Indeholder connection key
require('dotenv').config();
//Henter DB key
const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUniFiedTopology: true });
const connection = mongoose.connection;
connection.once('open', () => {
console.log('MongoDB database connetion established successfully');
});
const postRouter = require('./routes/posts.router');
const userRouter = require('./routes/user.router');
app.use('/posts', postRouter);
app.use('/user', userRouter);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.join(_dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Blog express server running on port: ${port}`);
});