-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
68 lines (57 loc) · 1.8 KB
/
App.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
const express = require("express")
const mongoose = require("mongoose")
const swaggerJSDoc = require('swagger-jsdoc')
const swaggerUi = require('swagger-ui-express')
const pino = require("pino")
const expressPino = require("express-pino-logger")
const logger = pino({level: process.env.LOG_LEVEL || "info", prettyPrint: true})
const expressLogger = expressPino({logger})
const path = require("path")
const config = require("config")
const router = require("./routes/auth.routes")
const postRouter = require("./routes/post.routes")
const PORT = config.get("port") || 8080
const app = express()
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'Express API for FAST-NEWS-FEED',
version: '1.0.0',
description:
'This is a REST API application made with Express.'
},
servers: [
{
url: 'http://localhost:8080',
description: 'Development server',
},
],
}
const options = {
swaggerDefinition,
apis: ['./routes/*.js'],
}
const swaggerSpec = swaggerJSDoc(options)
app.use(express.json({extended: true}))
app.use("/api/auth", router)
app.use("/api/post", postRouter)
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec))
app.use(expressLogger)
if (process.env.NODE_ENV === "production") {
app.use("/", express.static(path.join(__dirname, "client", "build")))
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"))
})
}
async function start() {
try {
await mongoose.connect(config.get("mongoUri"), {useNewUrlParser: true})
app.listen(PORT, () => {
logger.info(`Server running on port ${PORT}...`)
})
} catch (ex) {
logger.error(`Server error: ${ex.message}`)
process.exit(1)
}
}
start()