-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
64 lines (55 loc) · 1.82 KB
/
index.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 morgan = require('morgan')
const bodyParser = require('body-parser')
const mongoose = require('mongoose').set('debug', true)
const path = require('path')
require('dotenv').config()
const compression = require('compression')
const config = require('./config.js')
// Router Files
const adminRouter = require('./routes/admin')
const productRouter = require('./routes/product')
const orderRouter = require('./routes/order')
const logRouter = require('./routes/log')
const userRouter = require('./routes/user')
const authRouter = require('./routes/auth')
const env = process.env.NODE_ENV || 'development'
mongoose
.connect(env === 'development' ? config.DB_URI_DEV : config.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
socketTimeoutMS: 3000,
keepAlive: true,
reconnectTries: 3000,
})
.then(
function () {
//connected successfully
console.log('Database connection successful!')
},
function (err) {
console.log(err)
}
)
const app = express()
app.use(compression())
env !== 'development' &&
app.use(express.static(path.join(__dirname, 'client/build')))
env === 'development' && app.use(morgan('dev'))
app.use(bodyParser.json())
app.use('/api', adminRouter)
app.use('/api', productRouter)
app.use('/api', orderRouter)
app.use('/api', logRouter)
app.use('/api', userRouter)
app.use('/api', authRouter)
env !== 'development' &&
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
})
const port = process.env.PORT || 5000
app.listen(port, () => {
console.log(`Server running at ${port}`)
})