-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
43 lines (37 loc) · 1.28 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
require('dotenv').config({path:'./config/config.env'});
const express = require("express");
const bodyParser = require("body-parser");
const path = require('path');
const routes = require('./routes/routes.js');
const cookieParser = require('cookie-parser');
const app = express();
const cors= require('cors');
const connection = require('./db/connection');
//Connect to Database
connection();
app.use(express.urlencoded({
extended: true
}));
app.use(cors({credentials: true, origin: true, "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",}));
app.use(cookieParser());
app.use(express.json({ limit: '1mb' }));
app.use(bodyParser.json({ extended: true, limit: "50mb" }));
//Serve static files
if(process.env.NODE_ENV == 'production'){
app.use(express.static(path.resolve(__dirname, 'frontend', 'build')));
app.use('/api', routes);
app.get('/', (req, res)=>{
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
});
app.get('/*', (req, res)=>{
res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'));
});
};
app.get('/', (req, res)=>{
res.send("Server up and running");
})
app.use('/api', routes);
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log("Server started on port", PORT);
});