-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (74 loc) · 1.99 KB
/
index.js
File metadata and controls
84 lines (74 loc) · 1.99 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require("express");
const swaggerUI = require("swagger-ui-express");
const swaggerJsDoc = require("swagger-jsdoc");
const globalEmitter = require("./emitter/globalEmitter");
const connectToMongo = require("./middlewares/dbConnect");
const auth = require("./middlewares/auth.middleware");
const loginAccountLimiter = require("./middlewares/rateLimiter.middleware");
const logger = require("./middlewares/Logger.middleware");
require("dotenv").config();
//Swagger API Options
const options = {
definition: {
openapi: "3.0.0",
info: {
title: "Note API",
version: "1.0.0",
description: "A simple Express Note API",
},
components: {
securitySchemas: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
},
},
},
security: {
bearerAuth: [],
},
servers: [
{
url: "http://localhost:4000",
},
],
},
apis: ["./routes/*.js"],
};
//Instantiation
const specs = swaggerJsDoc(options);
const app = express();
//Middlewares
app.use(express.json());
//Custom Logger middleware
app.use(logger);
// Rate Limiter
app.use(loginAccountLimiter);
// Route-s
app.use("/api-docs", swaggerUI.serve, swaggerUI.setup(specs));
app.use("/notes", auth, require("./routes/note.routes"));
app.use("/", require("./routes/user.routes"));
//Events
//User Events
globalEmitter.on("login", (userId) => {
console.log(`User LoggedIn! id: ${userId}`);
});
globalEmitter.on("Signup", (userId) => {
console.log(`New user SignedUp! id: ${userId}`);
});
//Notes Events
globalEmitter.on("createNote", (noteId) => {
console.log(`Note Created having id: ${noteId}`);
});
globalEmitter.on("updateNote", (noteId) => {
console.log(`Note Updated having id: ${noteId}`);
});
globalEmitter.on("deleteNote", (noteId) => {
console.log(`Note Deleted having id: ${noteId}`);
});
app.listen(process.env["PORT"], () => {
connectToMongo().then(() => {
console.log("Port is Running");
});
});