javascript - Express js - Use routes in separate file - Stack Overflow

I'm trying to setup a node project and I want to put a file routes.js in routesroutes.js and cont

I'm trying to setup a node project and I want to put a file routes.js in routes/routes.js and controllers files in controllers/ directory.

So, for example I have the UserController like this:

var index = () =>
{
  console.log("User Index");
};
var getUser = (id) => {
  console.log("User by id " + id);
};

module.exports =
{
  index,
  getUser
}

And in routes.js I have this:

    var express = require('express');
var routes = express.Router();
var users = require('../controllers/usersController');


routes.route('/')
  .get(users.index);

routes.route('/user/:userId')
  .get(users.getUser);


module.exports=
{
  routes
};

And in index.js I'm setup in this way:

    let express = require('express');
let app = express();
let routes = require('./routes/routes');

app.set("views", './views');
app.set("view engine", 'jade');


app.use(express.static(__dirname + '/public'));

app.use('/', routes.index);
// launch ======================================================================
app.listen(9001);

When I try to run the server I have this error:

.../node_modules/express/lib/router/index.js:458
  throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
  ^
  TypeError: Router.use() requires a middleware function but got a undefined

What is the problem how can I configure setup this in this way?

Thank you

I'm trying to setup a node project and I want to put a file routes.js in routes/routes.js and controllers files in controllers/ directory.

So, for example I have the UserController like this:

var index = () =>
{
  console.log("User Index");
};
var getUser = (id) => {
  console.log("User by id " + id);
};

module.exports =
{
  index,
  getUser
}

And in routes.js I have this:

    var express = require('express');
var routes = express.Router();
var users = require('../controllers/usersController');


routes.route('/')
  .get(users.index);

routes.route('/user/:userId')
  .get(users.getUser);


module.exports=
{
  routes
};

And in index.js I'm setup in this way:

    let express = require('express');
let app = express();
let routes = require('./routes/routes');

app.set("views", './views');
app.set("view engine", 'jade');


app.use(express.static(__dirname + '/public'));

app.use('/', routes.index);
// launch ======================================================================
app.listen(9001);

When I try to run the server I have this error:

.../node_modules/express/lib/router/index.js:458
  throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
  ^
  TypeError: Router.use() requires a middleware function but got a undefined

What is the problem how can I configure setup this in this way?

Thank you

Share Improve this question asked Oct 31, 2017 at 11:52 user3242861user3242861 1,92912 gold badges53 silver badges97 bronze badges 2
  • did you copy paste this code? – Marco Salerno Commented Oct 31, 2017 at 11:54
  • In routes.js you never export anything called index, and thus in the line app.use('/', routes.index) inside of index.js, routes.index is undefined. You do however export something called routes (as in routes.routes), so maybe that's what you meant to use? – Khauri Commented Oct 31, 2017 at 11:57
Add a ment  | 

2 Answers 2

Reset to default 4

Your userController will be like this.

 module.exports = {
        index: (req, res) => {
            console.log("User Index");
        },
        getUser: (req, res) => {
            console.log("User by id " + req.params.id);
        }
    }

Your routes file will be like this

var express = require('express');
var routes = express.Router();
var users = require('../controllers/usersController');

routes.get('/', user.index);
routes.get('/user/:userId', user.getUser);

module.exports = routes;

Your index file will be like this

let express = require('express');
let app = express();
let routes = require('./routes/routes');

app.set("views", './views');
app.set("view engine", 'jade');


app.use(express.static(__dirname + '/public'));
app.use('/', routes);

app.listen(9001);

In routes.js replace:

module.exports=
{
  routes
};

to

module.exports = routes;

in index.js

app.use('/', routes.index); to app.use('/', routes);

should works...

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745346973a4623595.html

相关推荐

  • javascript - Express js - Use routes in separate file - Stack Overflow

    I'm trying to setup a node project and I want to put a file routes.js in routesroutes.js and cont

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信