All files / src app.ts

96.29% Statements 26/27
77.77% Branches 7/9
100% Functions 1/1
96.29% Lines 26/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                          7x   7x   7x           7x   7x 118x 118x 179x         118x     7x 7x 7x   7x     7x                                   7x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x      
import express, {Request, Response, NextFunction} from 'express';
import authRoutes from './routes/auth_routes';
import commentsRoutes from './routes/comments_routes';
import postsRoutes from './routes/posts_routes';
import usersRoutes from './routes/users_routes';
import swaggerUi, {JsonObject} from 'swagger-ui-express';
import swaggerJsdoc from 'swagger-jsdoc';
import options from './docs/swagger_options';
import {authenticateToken, authenticateTokenForParams} from "./middleware/auth";
import bodyParser from 'body-parser';
import roomsRoutes from './routes/rooms_routes';
import cors from 'cors';
import {config} from "./config/config";
import validateUser from "./middleware/validateUser";
import loadOpenApiFile from "./openapi/openapi_loader";
import resource_routes from './routes/resources_routes';
import resume_routes from './routes/resume_routes';
import githubRoutes from './routes/github_routes';
import quizzesRoutes from './routes/quizzes_routes';
import linkedinJobsRoutes from './routes/linkedin_jobs_routes';
 
const specs = swaggerJsdoc(options);
 
const app = express();
 
const corsOptions = {
    origin: [config.app.frontend_url(), config.app.backend_url()],
    methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
    credentials: true, // Allow cookies to be sent with requests
};
 
app.use(cors(corsOptions));
 
const removeUndefinedOrEmptyFields = (req: Request, res: Response, next: NextFunction) => {
    Eif (req.body && typeof req.body === 'object') {
        for (const key in req.body) {
            Iif (req.body[key] === undefined || req.body[key] === null || req.body[key] === '') {
                delete req.body[key];
            }
        }
    }
    next();
};
 
app.use(bodyParser.json());
app.use(removeUndefinedOrEmptyFields);
app.use(bodyParser.urlencoded({ extended: true }));
 
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(loadOpenApiFile() as JsonObject));
 
// Add Authentication for all routes except the ones listed below
app.use(authenticateToken.unless({
    path: [
        { url: '/auth/login' },
        { url: '/auth/social' },
        { url: '/auth/register' },
        { url: '/auth/refresh' },
        { url: '/auth/logout' },
        { url: /^\/post\/[^\/]+$/, methods: ['GET'] },  // Match /post/{anything} for GET
        { url: /^\/comment\/[^\/]+$/, methods: ['GET'] },  // Match /comment/{anything} for GET
        { url: /^\/comment\/post\/[^\/]+$/, methods: ['GET'] },  // Match /comment/post/{anything} for GET
        { url: '/comment', methods: ['GET'] },
        { url: '/post', methods: ['GET'] },  // Allow GET to /post
        { url: /^\/resource\/image\/[^\/]+$/, methods: ['GET'] },  // Allow GET to /resource/image/{anything}
    ]
}));
 
// Add AUTH middleware for params queries
// To block queries without Authentication
app.use(authenticateTokenForParams);
 
app.use('/auth', authRoutes);
app.use('/comment', commentsRoutes);
app.use('/post', postsRoutes);
app.use("/user/:id", validateUser);
app.use('/user', usersRoutes);
app.use('/resource', resource_routes);
app.use('/room', roomsRoutes);
app.use('/resume', resume_routes);
app.use('/github', githubRoutes);
app.use('/quiz', quizzesRoutes);
app.use('/linkedin-jobs', linkedinJobsRoutes);
 
export { app, corsOptions };