-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (110 loc) · 3.71 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require('dotenv').config({ path: "./.env"}) // install dotenv package first
const cors = require('cors');
const express = require('express');
const moesif = require('moesif-nodejs');
const bodyParser = require('body-parser');
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const moesifExpress = require('moesif-express');
// const moesifBrowser = require('moesif-browser-js');
const postRoutes = require('./routes/posts.route');
const userRoutes = require('./routes/users.route');
const app = express();
const port = process.env.PORT || 8080;
/* CORS Initialization - START */
const allowedOrigins = ['http://localhost:3000',
'http://localhost:3001',
'http://localhost:4000',
'http://localhost:8080',
'https://simple-node-backend-app.herokuapp.com/',
];
app.use(cors({
origin: function(origin, callback){
if(!origin) return callback(null, true); // Allows mobiles and tablets
if(allowedOrigins.indexOf(origin) === -1){
const msg = 'The CORS policy for this site does not ' +
'allow access from the Origin you are using.';
return callback(new Error(msg), false);
}
return callback(null, true);
}
}));
/* CORS Initialization - END */
app.use(bodyParser.json());
/* Moesif Initialization - START */
// moesifBrowser.init({
// applicationId: process.env.MOESIF_APPLICATION_ID,
// });
const moesifMiddleware = moesif({
applicationId: process.env.MOESIF_APPLICATION_ID,
// Optional hook to link API calls to users
identifyUser: function (req, res) {
return req.user ? req.user.id : undefined;
},
});
moesifMiddleware.startCaptureOutgoing();
app.use(moesifMiddleware);
/* Moesif Initialization - END */
/* Swagger Initialization - START */
const swaggerOption = {
swaggerDefinition: (swaggerJsdoc.Options = {
info: {
title: "first Node API",
description: " API documentation of my first Nodejs API",
contact: {
name: "Dewalade",
},
servers: ["http://localhost:8080/",'http://localhost:3001','http://localhost:4000','http://localhost:8080',"https://simple-node-backend-app.herokuapp.com/"],
},
}),
apis: ["index.js", "./routes/*.js"],
};
const swaggerDocs = swaggerJsdoc(swaggerOption);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
/* Swagger Initialization - END */
// HOME ENDPOINT
app.get('/', ( req , res ) => {
return res.status(200).send('Welcome to the first API homepage!')
})
/**
* @swagger
* /:
* get:
* description: Use this to test the first-API route
* tags:
* - home
* responses:
* '200':
* description: Test Successful
* '400':
* description: Bad Request
* '500':
* description: Internal Server Error
*/
// TEST ENDPOINT
app.get('/test', (req, res) => {
return res.status(200).send('Congrats! Test Successful. Welcome to the first-API backend!!');
});
/**
* @swagger
* /test:
* get:
* description: Use this to test the first-API route
* tags:
* - test
* responses:
* '200':
* description: Test Successful
* '400':
* description: Bad Request
* '500':
* description: Internal Server Error
*/
// MAIN API ENDPOINTS
app.use('/users', userRoutes);
app.use('/posts', postRoutes);
app.listen( port , () => {
console.log('\n[*] Starting first-node app');
console.log('[+] App is now running');
console.log('[+] Listening on port ' + port + '...');
});