First I am aware that the same exact questions appears here in Stackoverflow, so please bear with me.
So I am using node.js, express and mongoose and I get this error whenever I try to start the server using
node app
The error is
TypeError: Router.use() requires middleware function but got a Object
And tracing the error leads me to line 32 in app.js
TraceMSG
at Object.<anonymous> (C:\Proj-Phase2\app.js:32:5)
Line32:
let authRouter = require('./server/routes/authRoutes');
Now there is a similar question on stackoverflow
TypeError: Router.use() requires middleware function but got a Object
however the solution suggests a replacment of my line to be
app.use(app.router);
routes.initialize(app);
However this is already deprecated.
I printed my express version by printing the command
npm list express
And the result was
├── express@4.13.3
└─┬ lite-server@1.3.2
└─┬ browser-sync@2.10.1
└─┬ browser-sync-ui@0.5.16
└─┬ weinre@2.0.0-pre-I0Z7U9OV
└── express@2.5.11
AND here my app.js for Your reference
"use strict";
let express = require('express'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
open = require('open');
let dbConnection = mongoose.connect('mongodb://localhost/eRubric', function(err) {
if(!err) {
let instructorRepository = require('./server/models/instructorRepository');
instructorRepository.initDb();
}
});
//Initialize App
let app = express();
//Allow serving static files
app.use(express.static(__dirname));
//Define port
let port = process.env.PORT || 9080;
app.use(bodyParser.urlencoded({extended:true}));
//aut-deserialize the body of incoming request to a json object
app.use(bodyParser.json());
//Router Config
let authRouter = require('./server/routes/authRoutes');
//Replacement for recent express versions
app.use('/api/instructor/auth', authRouter);
//app.use(app.router);
//authRouter.initialize(app);
app.listen(port, function(){
console.log('Hero App is running my app on http://localhost:' + port);
//open('http://localhost:' + port);
});
AND HERE IS MY ROUTES file
"use strict";
let express = require('express');
let authController = require('../controllers/authController');
let authRouter = express.Router();
authRouter.route('/')
.post((req, res) => authController.authInstrucotr(req, res));