-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (31 loc) · 1.03 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
import express from 'express'
import cors from 'cors'
import mongoose from 'mongoose'
import config from 'config'
import corsMiddleware from './middleware/corsMiddleware.js'
import filepathMiddleware from './middleware/filepathMiddleware.js'
import router from './routes/router.js'
import path from 'path'
import fs from 'fs'
import fileUpload from 'express-fileupload'
const app = express()
const PORT = process.env.PORT || config.get('serverPort')
const DB_URL = config.get('dbUrl')
app.use(cors())
app.use(fileUpload({}))
app.use(filepathMiddleware(path.join(process.cwd(), 'data')))
app.use(express.json())
app.use(express.static(path.join(process.cwd(), 'data')))
app.use(router)
const start = async () => {
try {
await mongoose.connect(DB_URL, { useUnifiedTopology: true, useNewUrlParser: true })
app.listen(PORT, () => {
console.log('Server started on port ', PORT)
if (!fs.existsSync(path.join(process.cwd(), 'data'))) fs.mkdirSync(path.join(process.cwd(), 'data'))
})
} catch (e) {
console.log(e)
}
}
start()