-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (43 loc) · 2.12 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
// Import necessary External modules
import express from 'express';
import userAgent from 'express-useragent';
// Import necessary Core modules
import path from 'path';
// Import necessary internal modules
import userRouter from './src/features/user/user.routes.js';
import otpRouter from './src/features/otp/otp.routes.js';
import postRouter from './src/features/post/post.routes.js';
import likeRouter from './src/features/like/like.routes.js';
import commentRouter from './src/features/comment/comment.routes.js';
import authUser from './src/middlewares/auth-user.middleware.js';
import welcomeUser from './src/middlewares/welcomeUser.middleware.js';
import logRequest from './src/middlewares/logRequest.middleware.js';
import handleApplicationLevelErrors from './src/middlewares/applicationLevelErrorHandler.middleware.js';
import handleInvalidRoute from './src/middlewares/invalidRouteHandler.middleware.js';
import friendshipRouter from './src/features/friendship/friendship.routes.js';
// Create server using Express
const app = express();
app.use(express.json()); // Parse incoming JSON bodies
app.use(userAgent.express()); // Parse user-agent information
app.use(logRequest); // Log every request exept user routes
// Welcome user on home route
app.get('/api', welcomeUser);
// Serve post media
app.use('/uploads', express.static(path.join(path.resolve(), 'uploads')));
// Mount the userRouter for handling user related requests
app.use('/api/users', userRouter);
// Mount the otpRouter for handling otp related requests
app.use('/api/otp', otpRouter);
// Mount the postRouter for handling post related requests
app.use('/api/posts', authUser, postRouter);
// Mount the likeRouter for handling likes related requests
app.use('/api/likes', authUser, likeRouter);
// Mount the commentRouter for handling comments related requests
app.use('/api/comments', authUser, commentRouter);
// Mount the friendshipRouter for handling friendship related requests
app.use('/api/friends', authUser, friendshipRouter);
// Handle invalid routes
app.use(handleInvalidRoute);
// Handle all application errors
app.use(handleApplicationLevelErrors);
export default app;