-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·145 lines (120 loc) · 2.91 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
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
138
139
140
141
142
143
144
145
const morgan = require('morgan')
const express = require('express')
const {
userController
} = require('./components/user')
const {
groupController
} = require('./components/group')
const {
todoController
} = require('./components/todo')
const helmet = require('helmet')
const cors = require('cors')
const {
globalErrorHandler: errorHandler
} = require('./components/utilities')
const swaggerUi = require('swagger-ui-express')
const {
swaggerSpec,
knex
} = require('./configs')
const options = {
explorer: true
}
const app = express()
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, options))
app.use(cors())
app.use(express.urlencoded({
extended: false
}))
app.use(express.json())
app.use(morgan('combined'))
app.use(helmet())
app.use('/api/users', userController)
app.use('/api/groups', groupController)
app.use('/api/todos', todoController)
// 404
app.use((req, res) => {
// HTTP status 404: NotFound
res.status(404).json({
status: 'failed',
reason: '404 Not Found'
})
})
// All other errors are handled here
app.use(async (err, req, res, next) => {
// HTTP status 404: NotFound
// use centralized error handler here
const errorResult = await errorHandler(err)
// exit app if fatal
if (errorResult.fatal === true) {
console.log('fatal error')
await knex.destroy()
// FIXME: not best practice what about running jobs or transactions ?
// use a modified circuit breaker pattern to fix this - baad me krna
process.exit(1)
}
// other wise
res.status(errorResult.responseCode).json({
...errorResult.payload
})
})
process.addListener('unhandledRejection', async (err) => {
// log the error
// fatal
const errorResult = await errorHandler(err)
// exit app if fatal
if (errorResult.fatal === true) {
await knex.destroy()
process.exit(1)
}
})
process.addListener('uncaughtException', async (err) => {
// log the error
// fatal
const errorResult = await errorHandler(err)
// exit app if fatal
if (errorResult.fatal === true) {
await knex.destroy()
process.exit(1)
}
})
module.exports = {
app
}
// FIXME: There is a bug in pm2 run ap.js using no daemon
// and ctrcl-c. Run it again and then call npx pm2 stop ll
/*
Login
logout
using jwt or something else like session
View user info
Remove account action
Update account info
View all user todos
Filter todo by its content i.e title or desc
Filter todo by its status
Add todo
Update todo
Delete todo
CRUD on todo statuses
*/
/*
Understand sql more and learn sequelize fcking
Add validation middleware
Add Linter - DONE
Add formatter - DONE
Add precommit or pre push hooks to ensure formatting and linting
Add Authentication - using all kinds of methods and sheeet - Start with jwt cuz das easiest !
Restructor App
Learn more sql
Perform migrations
Learn nodejs
Add Swagger Docs
Learn advanced js
Do all the above in typescript
Dockerify this app
And add ci cd
And then deploy it
*/