简体   繁体   中英

app.use in express is causing error when connecting to a model and route

I am setting my routes and models in the app.js page. I am using several times. However when I reference my model and route like so

app.use('/api', require('./routes/api'));

I am getting the error

throw new TypeError('Router.use() requires middleware function but got a
            ^
TypeError: Router.use() requires middleware function but got a Object

Any reference I give to is giving me an error with app.use. 任何引用都给我app.use一个错误。 I am following the John Papa style guide. Some of my code on express is very similar. Seems to work fine in the hot towel generator but now working for me now. Wondering if it could be a different issue or if I am missing something specific.

I have required my models

var BeerList = require('./models/IBU');
var Beer = require('./models/beerName');

Here are the dependencies in my package.json Nothing out of the ordinary

"body-parser": "^1.8.2",
"express": "^4.12.2",
"express-session": "^1.11.3",
"mongoose": "^4.1.11",
"morgan": "^1.1.1",
"serve-favicon": "^2.0.1"

Any insight would be greatly appreciated.

Thanks

Try a different way. Towards the top of the file do:

var routes = require('./routes/api');

Then put this towards the bottom. It might be a location problem, as assigning routes should always go below other app.use's

app.use('/api', routes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM