简体   繁体   English

Router.use()需要中间件功能,但有一个对象

[英]Router.use() requires middleware function but got an Object

I was experimenting with Express 4's new Router and was wondering how one would go about doing below... 我正在尝试Express 4的新路由器,并且想知道如何在下面进行操作...

I made a file named app.js under a routes folder containing, basically, this: 我在routes文件夹下制作了一个名为app.js的文件,基本上包含以下内容:

var express = require('express');
var router = express.Router();

module.exports = (function() {

    router.get('/testindex', function(req, res) {
        res.render('testindex');
    })

    return router;

})();

In my server.js file this works as expected... 在我的server.js文件中,它按预期工作...

var appRouter = require('./routes/app');
var apiRouter = require('./routes/api/v1.js');
app.use('/', appRouter);
app.use('/api/v1',apiRouter);

I was hoping to do something like this, but I got the error I mentioned in the title... 我希望做这样的事情,但遇到标题中提到的错误...

var router = {
    api: {
        v1: require('./routes/api/v1'),
        v2: (...)
    },
    app: require('./routes/app')
};

app.use('/api/v1', router.api.v1);
app.use('/api/v2', router.api.v2);
app.use('/', router.app);

What am I not understanding? 我不明白什么?

When trying to reproduce your issue, there is no problem when I remove this statement: 尝试重现您的问题时,删除此语句没有任何问题:

app.use('/api/v2', router.api.v2);

So, there is probably something wrong with the way you defined the v2 property (which you did not flesh out in your question). 因此,您定义v2属性的方式可能存在问题(您在问题中并未充实)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Express 路由器:Router.use() 需要中间件 function 但得到了 Object - Express router : Router.use() requires a middleware function but got a Object EXPRESS:Router.use() 需要一个中间件函数,但得到了一个对象 - EXPRESS: Router.use() requires a middleware function but got a Object TypeError: Router.use() 需要中间件函数但得到了一个对象 - TypeError: Router.use() requires middleware function but got a Object Router.use()需要中间件功能,但在使用功能时未定义 - Router.use() requires middleware function but got a undefined at function use 使用 module.exports= router 但仍获得 Router.use() 需要中间件 function 但获得 Object - Using module.exports= router but still getting Router.use() requires a middleware function but got a Object Router.use() 需要一个中间件函数,但有一个未定义的 - Router.use() requires a middleware function but got a undefined 创建Router.use()的路由时出错,需要中间件功能 - Creating route got error of Router.use() requires middleware function Router.use() 需要中间件功能,但有一个未定义的 - Router.use() requires middleware function but got a undefined Router.use() 需要一个中间件 function 但得到一个字符串 - Router.use() requires a middleware function but got a string 如何解决“ Router.use()需要中间件功能,但在Function.use获得对象”错误 - How to solve “Router.use() requires a middleware function but got a Object at Function.use” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM