[英]Node.js: TypeError: Router.use() requires a middleware function but got a Object
[英]Router.use requires middleware function but got object : node.js / express
每當我嘗試使用app.use()初始化路由時,從路由目錄調用路由器時出現錯誤。 我得到的錯誤是說Router.use()需要一個中間件功能,但有一個對象。 我已經讀到這可能是由於我沒有正確導出模塊。 但是我不確定下面的代碼到底有什么問題。 我很確定錯誤是在我如何構造控制器。 我嘗試使用module.exports代替,但一直收到錯誤消息。 我該如何解決?
錯誤:
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';
使用import router from './routes/router';
然后用: 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 apiRoutes = express.Router(),
feedRoutes = express.Router(),
fileRoutes = express.Router(),
flightRoutes = express.Router(),
indexRoutes = express.Router();
//= ========================
// Feed Routes
//= ========================//
apiRoutes.use('/feeds', feed);
apiRoutes.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
//= ========================//
apiRoutes.use('/files', file);
// Find all files
fileRoutes.get('/', file.list_all_files);
// Find one file
fileRoutes.get('/:id', file.list_one_file);
//= ========================
// Flight Routes
//= ========================//
apiRoutes.use('/flights', flight);
// Find all flights
flightRoutes.get('/', flight.list_all_flights);
// Find one flight
flightRoutes.get('/:id', flight.list_one_flight);
//= ========================
// Index Routes
//= ========================//
// Set Main index Route
apiRoutes.use('/',
indexRoutes.get('/', function(req, res, next) { res.render('index', { title: 'Express', layout: 'layout.hbs' }) })
);
// Set url for API group routes
app.use('/', apiRoutes);
}
我的一個控制器的示例。 (都包含相同的模式):
import express from 'express'
import mongoose from 'mongoose'
// models
import Flight from '../models/flight.model'
exports.list_all_flights = function(req, res) {
Flight.find({})
.exec(function(err, dbflights){
if(err) {
res.send('error has occured in router');
} else {
console.log('successful get from routes/flights.js ...');
res.json(dbflights);
}
});
};
exports.list_one_flight = function(req, res) {
Flight.findOne({'name': req.params.id})
.exec(function(err, dbflights){
if(err){
res.send('get error has occured in routes/feeds.js');
} else {
res.json(dbflights);
}
});
};
除了掛載對象外,還可以掛載路由嗎? 因此,請替換:
apiRoutes.use('/feeds', feed);
與:
apiRoutes.use('/feeds', feedRoutes);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.