-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
99 lines (74 loc) · 2.72 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
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
const dotenv = require('dotenv')
//creating app
const app = express()
app.use(express.json())
// Connect to mongo
dotenv.config()
mongoose
.connect(`mongodb+srv://${process.env.MONGO_ATLAS_USER}:${process.env.MONGO_ATLAS_PASSWORD}@trail-mflro.mongodb.net/mydb`)
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.log(err))
// Init middleware
app.use(express.json())
app.use(express.urlencoded({extended: false}))
app.use(cors())
// Require Router Handlers
const articles = require('./routes/api/Articles')
const users = require('./routes/api/Users')
const debates = require('./routes/api/Debates')
const FAQs = require('./routes/api/FAQs')
const question = require('./routes/api/Questions')
const notification = require('./routes/api/Notifications')
const content = require('./routes/api/Contents')
const clubs = require('./routes/api/Clubs')
const chatbars = require('./routes/api/Chatbars')
// app.get('/articles', async (req, res) => {
// res.send(`<a href="/api/Articles">Articles</a>`)
// })
// app.get('/users',async (req, res) => {
// res.send(`<a href="/api/Users">Users</a>`)
// })
// app.get('/FAQs',async (req, res) => {
// res.send(`<a href="/api/FAQs">FAQs</a>`)
// })
// app.get('/Debates',async (req, res) => {
// res.send(`<a href="/api/Debates">Debates</a>`)
// })
// app.get('/Clubs', async (req, res) => {
// res.send(`<a href="/api/Clubs">Clubs</a>`)
// })
// app.get('/Content', async (req, res) => {
// res.send(`<a href="/api/Contents">Contents</a>`)
// })
app.use('/api/Users', users)
app.use('/api/Articles',articles)
app.use('/api/Debates', debates)
app.use('/api/FAQs', FAQs)
app.use('/api/Questions', question)
app.use('/api/Notifications', notification)
app.use('/api/Clubs', clubs)
app.use('/api/Contents', content)
app.use('/api/Chatbars', chatbars)
//Server static assets if in the production
if(process.env.NODE_ENV === 'production'){
app.use(express.static('client/build'));
app.get('*', (req, res)=>{
res.sendFile(path.resolve(__dirname,'client','build','index.html'));
})
}
// Entry point
// app.get('/', (req,res) => res.send(`<h1>Welcome to TIQ APP by ERROR 404</h1></br></br></br>
// <a href="/api/Articles">Articles</a> </br>
// <a href="/api/Users">Users</a> </br>
// <a href="/api/FAQs">FAQs</a> </br>
// <a href="/api/Debates">Debates</a> </br>
// <a href="/api/Clubs">Clubs</a> </br>
// <a href="/api/Contents">Contents</a>`))
app.use((req, res) => {
res.status(404).send({err: 'We can not find what you are looking for'});
})
const port = process.env.PORT || 5000
app.listen(port, () => console.log(`Server on ${port}`))