表达:Router.use()需要中间件功能,但有一个对象 [英] EXPRESS: Router.use() requires a middleware function but got a Object

查看:377
本文介绍了表达:Router.use()需要中间件功能,但有一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试使用app.use()初始化路由时,从路由目录调用路由器时出现错误.我得到的错误是说Router.use()需要一个中间件功能,但有一个对象.我已经读到这可能是由于我没有正确导出模块.但是我不确定下面的代码到底有什么问题.

I am getting an error when calling my router from my routes directory whenever i try to initialize a route with app.use(). The error i am getting says Router.use() requires a middleware function but got a Object. I've read that this could be due to me not exporting the module correctly. However i am not sure what exactly is wrong with my code below.

错误:

Users/name/Desktop/personal/app/server/src/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 Object
    at Function.use (/Users/name/Desktop/personal/app/server/src/node_modules/express/lib/router/index.js:458:13)
    at Function.<anonymous> (/Users/name/Desktop/personal/app/server/src/node_modules/express/lib/application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (/Users/name/Desktop/personal/app/server/src/node_modules/express/lib/application.js:217:7)
    at module.exports (/Users/name/Desktop/personal/app/server/src/routes/router.js:18:7)
    at Object.<anonymous> (/Users/name/Desktop/personal/app/server/src/app.js:60:1)
    at Module._compile (module.js:660:30)
    at loader (/Users/name/Desktop/personal/app/server/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/name/Desktop/personal/app/server/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at Object.<anonymous> (/Users/name/Desktop/personal/app/server/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
[nodemon] app crashed - waiting for file changes before starting...

我的route.js文件,其中包含我的所有route方法.在app.js中:我使用import router from './routes/router';,然后用以下代码调用:router(app);

My route.js file that contains all my route methods. In app.js: I use import router from './routes/router'; then call this with: router(app);

import express from 'express'
import feed from '../controllers/feed.controller'
import file from '../controllers/file.controller'
import flight from '../controllers/flight.controller'

module.exports = function (app) {

  // Initializing route groups
  const feedRoutes = express.Router(),
    fileRoutes = express.Router(),
    flightRoutes = express.Router(),
    indexRoutes = express.Router();

  //= ========================
  // Feed Routes
  //= ========================//

  app.use('/feeds', feed);

  app.use('/downloads', feed);

  // Find all feeds
  feedRoutes.get('/', feed.list_all_feeds);

  // Find one feed
  feedRoutes.get('/:id', feed.list_one_feed);

  // Find one Audience
  feedRoutes.get('/:id/:audienceId', feed.list_one_feed_audience);

  // Download one Feed
  feedRoutes.get('/:id/download', feed.download_one_feed);

  //= ========================
  // File Routes
  //= ========================//

  app.use('/files', file);

  // Find all files
  fileRoutes.get('/', file.list_all_files);

  // Find one file
  fileRoutes.get('/:id', file.list_one_file);

  //= ========================
  // Flight Routes
  //= ========================//

  app.use('/flights', flight);

  // Find all flights
  flightRoutes.get('/', flight.list_all_flights);

  // Find one flight
  flightRoutes.get('/:id', flight.list_one_flight);

  //= ========================
  // Index Routes
  //= ========================//

  app.use('/', index);

  // Set Main index Route
  indexRoutes.get('/', function(req, res, next) { res.render('index', { title: 'Express', layout: 'layout.hbs' }) });
}

推荐答案

错误提供了详细信息

at module.exports (/Users/name/Desktop/personal/app/server/src/routes/router.js:18:7)

app.use('/feeds', feed);

feed这是一个对象而不是一个函数

feed here is an object and not a function

这篇关于表达:Router.use()需要中间件功能,但有一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆