• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

profcfuhrmanets / log210-systeme-gestion-bordereaux / 11076279501

27 Sep 2024 07:03PM UTC coverage: 98.333% (-1.0%) from 99.322%
11076279501

push

github

web-flow
Merge pull request #38 from profcfuhrmanets/fuhrmanator/issue35

Générer un message d'erreur pour indiquer qu'il s'agit de la doc qui manque

28 of 31 branches covered (90.32%)

Branch coverage included in aggregate %.

3 of 4 new or added lines in 1 file covered. (75.0%)

267 of 269 relevant lines covered (99.26%)

36.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.38
/src/App.ts
1
import express = require('express');
16✔
2
import { Request, Response, NextFunction } from 'express';
3
import logger = require('morgan');
16✔
4
const path = require('path')
16✔
5

6
import { courseRouter } from './routes/CourseRouter';
16✔
7
import { scheduleRouter } from './routes/ScheduleRouter';
16✔
8
import { semesterRouter } from './routes/SemesterRouter';
16✔
9
import { studentRouter } from './routes/StudentRouter';
16✔
10
import { teacherRouter } from './routes/TeacherRouter';
16✔
11
import { healthRouter as healthRouter } from './routes/HealthRouter';
16✔
12
import { gradeRouter } from './routes/GradeRouter';
16✔
13
import AbstractError from './errors/AbstractError';
16✔
14
import { InternalError } from './errors/InternalError';
16✔
15
import { NotFoundError } from './errors/NotFoundError';
16✔
16

17
/**
18
 * Creates and configures an ExpressJS web server
19
 */
20
class App
21
{
22
    public express: express.Application;
23

24
    /**
25
     * Run configuration methods on the Express instance
26
     */
27
    constructor()
28
    {
29
        this.express = express();
16✔
30
        this.middleware();
16✔
31
        this.routes();
16✔
32
        this.express.use(this.errorHandler);
16✔
33
    }
34

35
    /**
36
     * Configure Express middlewares
37
     */
38
    private middleware(): void
39
    {
40
        const staticFilesPath = path.resolve(__dirname, '../dist');
16✔
41
        console.log('Serving static files from:', staticFilesPath);
16✔
42
        this.express.use(express.static(staticFilesPath));
16✔
43
        this.express.use(logger('dev'));
16✔
44
        this.express.use(express.json());
16✔
45
        this.express.use(express.urlencoded({ extended: false }));
16✔
46
    }
47

48
    /**
49
     * Configure API endpoints
50
     */
51
    private routes(): void
52
    {
53
        let router = express.Router();
16✔
54

55
        router.get('/', function (req, res)
16✔
56
        {
57
            res.redirect('docs/index.html');
2✔
58
        });
59

60
        this.express.use('/api/v3/health', healthRouter.router);
16✔
61
        this.express.use('/api/v3/course', courseRouter.router)
16✔
62
        this.express.use('/api/v3/schedule', scheduleRouter.router)
16✔
63
        this.express.use('/api/v3/semester', semesterRouter.router)
16✔
64
        this.express.use('/api/v3/student', studentRouter.router)
16✔
65
        this.express.use('/api/v3/teacher', teacherRouter.router)
16✔
66
        this.express.use('/api/v3/grade', gradeRouter.router)
16✔
67
        this.express.use('/docs', express.static(path.join(__dirname, 'docs')));
16✔
68
        this.express.use('/static', express.static(path.join(__dirname, 'public')));
16✔
69
        this.express.use('/', router);
16✔
70
        this.express.use(function(req:Request, res:Response)
16✔
71
        {
72
            let errorMessage = "La route demandée n'existe pas.";
2✔
73
            if (req.path.startsWith('/docs')) {
2!
NEW
74
                errorMessage += " Veuillez noter que la documentation doit être générée la première fois avec la commande `npm run all_docs`.";
×
75
            }
76
            throw new NotFoundError(errorMessage);    
2✔
77
        });
78
    }
79

80
    /**
81
     * Logs the errors in the console and sends them as JSON to the caller
82
     * @param error The error to handle
83
     * @param req The request object
84
     * @param res The response object
85
     * @param next The next function to call
86
     */
87
    private errorHandler(error:Error, req:Request, res:Response, next:NextFunction)
88
    {
89
        var myError:AbstractError;
90

91
        if (error instanceof AbstractError)
14!
92
        {
93
            myError = error;
14✔
94
        }
95
        else
96
        {
97
            myError = new InternalError();
×
98
        }
99

100
        console.log(error);
14✔
101
        res.status(myError.code).json({"error": myError});
14✔
102
    }
103
}
104

105
export default new App().express;
16✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc