Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (58 loc) · 1.66 KB
/
Copy pathserver.js
File metadata and controls
66 lines (58 loc) · 1.66 KB
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
import express from 'express';
import 'dotenv/config'
import cors from 'cors';
import { conn } from "./db/conn.js";
import analyticsRouter from "./routes/analyticsRoutes.js";
import swaggerJsdoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';
const app = express();
const port = 3000 || process.env.PORT;
conn();
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'Notifications analytics API',
version: '1.0.0',
description: 'API documentation',
},
externalDocs: {
url: "/swagger.json"
},
},
apis: ['server.js', './routes/analyticsRoutes.js'],
};
const swaggerSpec = swaggerJsdoc(swaggerOptions);
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.get('/swagger.json', (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
app.get('/v3/api-docs', (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/analytics", analyticsRouter);
app.use((err, _req, res, next) => {
res.status(500).send("An unexpected error occurred.")
});
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});
/**
* @swagger
* /health:
* get:
* summary: Check if the service is running.
* responses:
* 200:
* description: Service is running.
* 500:
* description: Service is not running.
*/
app.get('/health', async (req, res) => {
res.status(200).json('Api is up and running');
});