-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapi.js
More file actions
223 lines (170 loc) · 5.9 KB
/
api.js
File metadata and controls
223 lines (170 loc) · 5.9 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
const express = require('express');
const studentsrouter = require('./students/students.router');
const programRouter = require('./progams/program.router')
const userRouter = require('./users/users.router')
const viewRouter = require('./views/views.router')
const UserModel = require('./models/user.model');
const morgan = require('morgan');
const logger = require('./logger');
const redisClient = require('./integrations/redis');
const fs = require('fs');
const { profile } = require('console');
const rateLimit = require('express-rate-limit');
// multer
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })
const cloudinary = require('./integrations/cloudinary');
const app = express()
// x-device-id
// configure rate limiter
const limiter = rateLimit({
windowMs: 1 * 60 * 1000, // 24 hrs
limit: 5, // Limit each IP to 100 requests per `window` (here, per 1 minutes).
keyGenerator: function (req) {
return req.headers['x-device-id'] || req.user.id || req.ip
},
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header.
});
// (req, res, next) => {
// Apply the rate limiting middleware to all requests.
// app.use(limiter)
app.use(morgan('dev'));
app.use(express.json()) // body parser
app.use(express.urlencoded({ extended: true })); // body prser: formdata
app.set('view engine', 'ejs')
app.use('/views', viewRouter)
// app.post('/file/upload', upload.single('photo'), async (req, res, next) => {
// console.log(req.file)
// // return response
// return res.json({
// message: 'file uploaded successfully',
// error: null
// })
// })
app.post('/file/upload', upload.single('photo'), async (req, res, next) => {
try {
console.log(req.file)
// upload file to cloudinary
const cloudinaryResponse = await cloudinary.uploader.upload(req.file.path)
// delete file from file directory
fs.unlink(req.file.path, (err) => {
if (err) {
console.error(err)
return
}
})
// response
const resexample = {
"asset_id": "2c0911e74485d4fa3f274563b053286c",
"public_id": "kr7woaeblwlautl3k53n",
"version": 1715193094,
"version_id": "5bf3e0b0198260fa23c1dea12bd95ffa",
"signature": "368049724ff3c96cd01938614cef21ba45f30c68",
"width": 1600,
"height": 900,
"format": "png",
"resource_type": "image",
"created_at": "2024-05-08T18:31:34Z",
"tags": [],
"bytes": 726191,
"type": "upload",
"etag": "7168a51a422beac728016f7f2f9f0627",
"placeholder": false,
"url": "http://res.cloudinary.com/dt40xxwe4/image/upload/v1715193094/kr7woaeblwlautl3k53n.png",
"secure_url": "https://res.cloudinary.com/dt40xxwe4/image/upload/v1715193094/kr7woaeblwlautl3k53n.png",
"folder": "",
"original_filename": "b6bfbf1dd75dff535c149c5a7f0b4b03",
"api_key": "992659252339482"
}
// return response
return res.json({
data: cloudinaryResponse,
error: null
})
} catch (error) {
return res.status(500).json({
data: null,
error: 'Server Error',
errorData: error
})
}
})
const homeRouteLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minutes
limit: 1000, // Limit each IP to 100 requests per `window` (here, per 1 minutes).
standardHeaders: 'draft-7', // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header.
});
// home route
app.get('/', homeRouteLimiter, (req, res) => {
logger.info("Home route accessed")
return res.status(200).json({ message: 'success', status: true })
})
// application caching layer
// database caching layer
app.get('/users', async (req, res) => {
const query = req.query
const whereQuery = {};
const limit = query.per_page || 10
const page = query.page || 0
if (query.name) {
whereQuery.name = query.name
}
if (query.email) {
whereQuery.email = query.email
}
// check cache
const cacheKey = `users:${JSON.stringify(whereQuery)}:${limit}:${page}`
// get data from database
const data = await redisClient.get(cacheKey)
// cache miss
if (data) {
console.log('returning data from cache')
return res.json({
data: JSON.parse(data),
error: null
})
}
console.log('returning data from database')
const users = await UserModel.find(whereQuery).limit(limit).skip(page).exec()
// set cache
await redisClient.setEx(cacheKey, 1 * 60 ,JSON.stringify(users))
return res.json({
users
})
})
app.post('/users', async (req, res) => {
await redisClient.del('/users')
await UserModel.create(req.body)
await UserModel.update()
})
// app.get('/', limiter, (req, res) => res.json({ message: 'Welcome'}))
// GET http://localhost/students
// GET http://localhost/students/134/department
// GET http://localhost/students/134/program
// POST http://localhost/students
// PATCH http://localhost/students/123
// DELETE http://localhost/students/123
app.use('/students', studentsrouter)
// GET http://localhost/programs
// GET http://localhost/programs/134/department
// GET http://localhost/programs/134/program
// POST http://localhost/programs
// PATCH http://localhost/programs/123
// DELETE http://localhost/programs/123
app.use('/programs', programRouter)
app.use('/users', userRouter)
app.get('*', (req, res) => {
return res.status(404).json({
data: null,
error: 'Route not found'
})
})
// global error handler
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).json({
data: null,
error: 'Server Error'
})
})
module.exports = app;