Issues (17)

1
const express = require("express");
2
const bodyParser = require('body-parser');
3
const cors = require('cors');
4
const morgan = require('morgan');
5
6
const app = express();
7
const port = 1337;
8
9
const index = require('./routes/index');
10
const reports = require('./routes/reports');
11
const week = require('./routes/week');
12
const register = require('./routes/register');
13
const login = require('./routes/login');
14
15
app.use(cors());
16
17
// This is middleware called for all routes.
18
// Middleware takes three parameters.
19
//app.use((req, res, next) => {
20
//    console.log(req.method);
21
//    console.log(req.path);
22
//    next();
23
//});
24
25
// don't show the log when it is test
26
if (process.env.NODE_ENV !== 'test') {
27
    // use morgan to log at command line
28
    app.use(morgan('combined')); // 'combined' outputs the Apache style LOGs
29
}
30
31
// for parsing application/json
32
app.use(bodyParser.json());
33
// for parsing application/x-www-form-urlencoded
34
app.use(bodyParser.urlencoded({ extended: true }));
35
36
// Imported routes
37
app.use('/', index);
38
app.use('/reports/', reports);
39
app.use('/reports/week/', week);
40
app.use('/register/', register);
41
app.use('/login/', login);
42
43
// Catch 404 and forward to error handler
44
app.use((req, res, next) => {
45
    var err = new Error("Not Found");
46
    err.status = 404;
47
    next(err);
48
});
49
50
app.use((err, req, res, next) => {
51
    if (res.headersSent) {
52
        return next(err);
53
    }
54
55
    res.status(err.status || 500).json({
56
        "errors": [
57
            {
58
                "status": err.status,
59
                "title":  err.message,
60
                "detail": err.message
61
            }
62
        ]
63
    });
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
64
});
65
66
// Start up server
67
// if(!module.parent){
68
//     app.listen(process.env.PORT, () =>
69
//         console.log(`Example app listening on port ${process.env.PORT}!`),
70
//     );
71
// }
72
73
const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`));
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
74
75
module.exports = server;
76