Express: TypeError: Router.use() requires middleware function but got a Object

Created on 8 Apr 2017  Â·  2Comments  Â·  Source: expressjs/express

macOS Sierra 10.12.4
node v7.8.0
npm 4.2.0

index.js

const express = require('express')
const bodyParser = require('body-parser')
const session = require('express-session')
const mongoose = require('mongoose')
const MongoStore = require('connect-mongo')(session)
const morgan = require('morgan')
const csrf = require('lusca').csrf()
const fs = require('fs')
const path = require('path')
const rfs = require('rotating-file-stream')

mongoose.connect('mongodb://localhost/test')

const app = express()

const logDir = path.join(__dirname, 'log')
fs.existsSync(logDir) || fs.mkdirSync(logDir)
const accessLogStream = rfs('access.log', {
  interval: '1d',
  path: logDir
})

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(session({
  secret: 'ohmy411',
  store: new MongoStore({
    mongooseConnection: mongoose.connection
  })
}))
app.use(morgan('combined'), { stream: accessLogStream })
app.use((req, res, next) => csrf(req, res, next))

app.get('/', (req, res) => res.send('hello world'))

app.listen(3000)

error info

➜  server (master) ✗ node index.js
express-session deprecated undefined resave option; provide resave option index.js:26:9
express-session deprecated undefined saveUninitialized option; provide saveUninitialized option index.js:26:9
/Users/simonqian/Work/ohmy411/server/node_modules/express/lib/router/index.js:458
      throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
      ^

TypeError: Router.use() requires middleware function but got a Object
    at Function.use (/Users/simonqian/Work/ohmy411/server/node_modules/express/lib/router/index.js:458:13)
    at Function.<anonymous> (/Users/simonqian/Work/ohmy411/server/node_modules/express/lib/application.js:220:21)
    at Array.forEach (native)
    at Function.use (/Users/simonqian/Work/ohmy411/server/node_modules/express/lib/application.js:217:7)
    at Object.<anonymous> (/Users/simonqian/Work/ohmy411/server/index.js:32:5)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:423:7)
    at startup (bootstrap_node.js:147:9)
    at bootstrap_node.js:538:3

dependencies

  {
    "body-parser": "^1.17.1",
    "connect-mongo": "^1.3.2",
    "express": "^4.15.2",
    "express-session": "^1.15.2",
    "lusca": "^1.4.1",
    "mongoose": "^4.9.2",
    "morgan": "^1.8.1",
    "rotating-file-stream": "^1.2.1",
    "uuid": "^3.0.1"
  }

Where is it wrong?

4.x question

Most helpful comment

here:

app.use(morgan('combined'), { stream: accessLogStream })

seems like you are trying to use 2 middleware where the second is a Object { stream: accessLogStream }. you should have:

app.use(morgan('combined', { stream: accessLogStream }))

All 2 comments

here:

app.use(morgan('combined'), { stream: accessLogStream })

seems like you are trying to use 2 middleware where the second is a Object { stream: accessLogStream }. you should have:

app.use(morgan('combined', { stream: accessLogStream }))

@willyelm its working now thank you so much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

haider0324 picture haider0324  Â·  3Comments

afanasy picture afanasy  Â·  3Comments

zackarychapple picture zackarychapple  Â·  3Comments

wxs77577 picture wxs77577  Â·  3Comments

jefflage picture jefflage  Â·  4Comments