-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
64 lines (48 loc) · 1.82 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
const express = require('express');
const cors = require('cors');
const path = require('path');
const socket = require('socket.io');
const mongoose = require('mongoose');
require('dotenv').config();
const helmet = require('helmet')
const app = express();
const NODE_ENV = process.env.NODE_ENV;
let dbUri = '';
if (NODE_ENV === 'production') dbUri = `mongodb+srv://davidwierzycki:${process.env.DB_PASS}@wierza.cd9ozgu.mongodb.net/NewWaveDB?retryWrites=true&w=majority`;
else if (NODE_ENV === 'test') dbUri = 'mongodb://127.0.0.1:27017/companyDBtest';
else dbUri = `mongodb+srv://davidwierzycki:${process.env.DB_PASS}@wierza.cd9ozgu.mongodb.net/NewWaveDB?retryWrites=true&w=majority`
mongoose.connect(dbUri, { useNewUrlParser: true });
const db = mongoose.connection;
db.once('open', () => {
console.log('Connected to the database');
});
db.on('error', err => console.log('Error ' + err));
const server = app.listen(process.env.PORT || 8000, () => {
console.log('Server is running on port: 8000');
});
const io = socket(server);
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(helmet())
app.use((req, res, next) => {
req.io = io;
next();
});
const testimonialsRoutes = require('./routes/testimonials.routes')
const concertsRoutes = require('./routes/concerts.routes');
const seatsRoutes = require('./routes/seats.routes');
app.use('/api', testimonialsRoutes);
app.use('/api', concertsRoutes);
app.use('/api', seatsRoutes);
app.use(express.static(path.join(__dirname, '/client/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/client/build/index.html'));
});
io.on('connection', socket => {
console.log('New client! Its id – ' + socket.id);
});
app.use((req, res) => {
res.status(404).json({ message: 'Not found...' });
});
module.exports = server;