app.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 2

Size

Lines of Code 80
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 24
dl 0
loc 80
rs 10
wmc 10
mnd 1
bc 6
fnc 5
bpm 1.2
cpm 2
noi 3

3 Functions

Rating   Name   Duplication   Size   Complexity  
A app.use 0 5 1
A ➔ methodOverride 0 9 4
A ➔ ??? 0 1 1
1
const config = require('./config')
2
const express = require('express')
3
const path = require('path')
4
const logger = require('./config/logger')
5
const morgan = require('morgan')
6
const session = require('express-session')
7
const cookieParser = require('cookie-parser')
8
const methodOverride = require('method-override')
9
const bodyParser = require('body-parser')
10
const flash = require('connect-flash')
11
const mongoose = require('mongoose')
12
13
const app = express()
14
15
require('./models')(app)
16
17
/**
18
 * API keys and Passport configuration.
19
 */
20
const passport = require('passport')
21
require('./config/passport')
22
23
// connect to Mongo when the app initializes
24
mongoose.connect(config.db)
25
26
// view engine setup
27
app.set('views', path.join(__dirname, 'views'))
28
app.set('view engine', 'pug')
29
30
// uncomment after placing your favicon in /public
31
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
32
if (config.env === 'development') {
33
  app.use(morgan('dev', { stream: { write: message => logger.info(message) } }))
34
} else {
35
  app.use(morgan('combined', { stream: { write: message => logger.info(message) } }))
36
}
37
app.use(bodyParser.json())
38
app.use(bodyParser.urlencoded({ extended: true }))
39
app.use(methodOverride(function(req, res){
0 ignored issues
show
Unused Code introduced by
The parameter res is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
40
      if (req.body && typeof req.body === 'object' && '_method' in req.body) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if req.body && typeof req.b...& "_method" in req.body is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
41
        logger.info('In method override')
42
        // look in urlencoded POST bodies and delete it
43
        var method = req.body._method
44
        delete req.body._method
45
        return method
46
      }
47
}))
48
app.use(cookieParser())
49
app.use(session({
50
  secret: config.cookieSecret,
51
  name: config.sessionName,
52
  proxy: true,
53
  resave: true,
54
  saveUninitialized: true
55
}))
56
app.use(flash())
57
app.use('/static', express.static(path.join(__dirname, 'public')))
58
app.use(passport.initialize())
59
app.use(passport.session())
60
require('./config/routes')(app)
61
62
// catch 404 and forward to error handler
63
app.use(function (req, res, next) {
64
  var err = new Error('Not Found')
65
  err.status = 404
66
  next(err)
67
})
68
69
// error handler
70
app.use(function (err, req, res, next) {
0 ignored issues
show
Unused Code introduced by
The parameter next is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
71
  // set locals, only providing error in development
72
  res.locals.message = err.message
73
  res.locals.error = req.app.get('env') === 'development' ? err : {}
74
  logger.error(err.stack)
75
  // render the error page
76
  res.status(err.status || 500)
77
  res.render('error')
78
})
79
80
module.exports = app
81