Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am getting this error when I run npm start to run my express app.

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

my app.js code

var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser');  var routes = require('./routes/index'); var users = require('./routes/users');  var app = express();  // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs');  // uncomment after placing your favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public')));  app.use('/', routes); app.use('/users', users);    /// catch 404 and forwarding to error handler app.use(function(req, res, next) {     var err = new Error('Not Found');     err.status = 404;     next(err); });  /// error handlers  // development error handler // will print stacktrace if (app.get('env') === 'development') {     app.use(function(err, req, res, next) {         res.status(err.status || 500);         res.render('error', {             message: err.message,             error: err         });     }); }  // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) {     res.status(err.status || 500);     res.render('error', {         message: err.message,         error: {}     }); });  module.exports = app; 

my index.js code

var express = require('express'); var router = express.Router();  /* GET home page. */ router.get('/', function(req, res) {     res.render('index', { title: 'Express' }); });  /* GET Hello World page. */ router.get('/helloworld', function(req, res) {     res.render('helloworld', { title: 'Hello, World!' }) });  module.exports = router; 

I am quirte new to using Node and express. I cant see where I have gone wrong. Can anybody see what my problem is?

like image 994
MattClaff Avatar asked Apr 29 '15 10:04

MattClaff


People also ask

What is middleware function?

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application's request-response cycle. These functions are used to modify req and res objects for tasks like parsing request bodies, adding response headers, etc.

How do I declare a router in node JS?

Routing with Express in Node: Express. js has an “app” object corresponding to HTTP. We define the routes by using the methods of this “app” object. This app object specifies a callback function, which is called when a request is received.

What is router use?

A router receives and sends data on computer networks. Routers are sometimes confused with network hubs, modems, or network switches. However, routers can combine the functions of these components, and connect with these devices, to improve Internet access or help create business networks.

What is app use in Express JS?

The app. use() method mounts or puts the specified middleware functions at the specified path. This middleware function will be executed only when the base of the requested path matches the defined path.


Video Answer


2 Answers

I found the answer in the comments from Kop4lyf:

check your users.js. It should also be exporting the router like index.js, if you can try that.

However, this question was my top search result when I ran into this issue, so I am promoting to an answer.


The error is caused because one of your route modules is not being exported - meaning Express does not have access to it when it tries to identify all of your routes.

You can fix this by adding module.exports = router; to the end of each of your route files.

Example:

var express = require('express'); var router = express.Router();  router.get('/', function(req, res, next) {      //Do whatever... });  module.exports = router; 

More information about module.exports can be found on this question or the offcial Node.js documentation.

like image 62
HPierce Avatar answered Sep 20 '22 15:09

HPierce


I have fixed this by adding which i am using somewhere. So please check your all exports.

module.exports = router; 
like image 26
SUBHASIS MONDAL Avatar answered Sep 17 '22 15:09

SUBHASIS MONDAL