0

After I have added a route to my express handler, I am getting an error (see below):

/app/node_modules/express/lib/router/index.js:458
2017-01-03T15:53:48.842543+00:00 app[web.1]:       throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
2017-01-03T15:53:48.842545+00:00 app[web.1]:       ^
2017-01-03T15:53:48.842545+00:00 app[web.1]: 
2017-01-03T15:53:48.842546+00:00 app[web.1]: TypeError: Router.use() requires middleware function but got a Object
2017-01-03T15:53:48.842547+00:00 app[web.1]:     at Function.use (/app/node_modules/express/lib/router/index.js:458:13)
2017-01-03T15:53:48.842548+00:00 app[web.1]:     at EventEmitter.<anonymous> (/app/node_modules/express/lib/application.js:219:21)
2017-01-03T15:53:48.842549+00:00 app[web.1]:     at Array.forEach (native)
2017-01-03T15:53:48.842549+00:00 app[web.1]:     at EventEmitter.use (/app/node_modules/express/lib/application.js:216:7)
2017-01-03T15:53:48.842550+00:00 app[web.1]:     at module.exports (/app/server/routes/index.js:16:9)
2017-01-03T15:53:48.842551+00:00 app[web.1]:     at Object.<anonymous> (/app/server.js:59:27)
2017-01-03T15:53:48.842551+00:00 app[web.1]:     at Module._compile (module.js:556:32)
2017-01-03T15:53:48.842552+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:565:10)
2017-01-03T15:53:48.842553+00:00 app[web.1]:     at Module.load (module.js:473:32)
2017-01-03T15:53:48.842553+00:00 app[web.1]:     at tryModuleLoad (module.js:432:12)
2017-01-03T15:53:48.842554+00:00 app[web.1]:     at Function.Module._load (module.js:424:3)
2017-01-03T15:53:48.842554+00:00 app[web.1]:     at Module.runMain (module.js:590:10)
2017-01-03T15:53:48.842555+00:00 app[web.1]:     at run (bootstrap_node.js:394:7)
2017-01-03T15:53:48.842556+00:00 app[web.1]:     at startup (bootstrap_node.js:149:9)
2017-01-03T15:53:48.842556+00:00 app[web.1]:     at bootstrap_node.js:509:3

This is the my index.js for routing (just a part of it):

module.exports = function(app) {
...
    app.use('/api/elasticsearch', require('../elasticsearch'));
...

And here the index.js in folder "elasticsearch":

var express = require('express');
var controller = require('./elasticsearch.controller');
var router = express.Router();

router.post('/index', controller.writeIndex);

module.exports = router;

Here is the start of the "writeIndex" function within the controller, I do not see any errors there as well:

exports.writeIndex = function (req, res) {
..
}
function indexData(technicians,indexName, typeName) {
..
}
..

I am using HEROKU, and I did not have any problems before. I think the main error here is:

throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));

but what does it mean?

Here is my directory structure of my project (just a part of it to be readable):

- server
-- elasticsearch
-- index.js
-- elasticsearch.controller.js
- routes
-- index.js
- api
-- coresystems
-- coresystems.controller.js
-- coresystemsAPI.js
-- index.js
Pille
  • 1,123
  • 4
  • 16
  • 38
  • It means you are passing an object rather than a middleware function to your route. How is "require('../elasticsearch')" defined? What does the modules.export in that file actually export? (Hint: its exporting an object not a function) –  Jan 03 '17 at 16:13
  • This "require" requires a directory, therefor should call an index.js in the directory "elasticsearch", the file I posted above. – Pille Jan 03 '17 at 16:15
  • BTW: When i use "app.use" the route I am stating is NOT the route in the file system but some I want to use to be usable for requests, right? – Pille Jan 03 '17 at 16:17
  • /app/server/routes/index.js:16:9 <- Which line of code posted is this? –  Jan 03 '17 at 16:18
  • It is the added route: app.use('/api/elasticsearch', require('../elasticsearch')); I added the directory structure for clarification. – Pille Jan 03 '17 at 16:20
  • Also, shouldn't exports.writeIndex be module.exports.writeIndex? http://stackoverflow.com/questions/7137397/module-exports-vs-exports-in-node-js –  Jan 03 '17 at 16:20
  • This should not make a difference, I used that syntax before on other places in my project. – Pille Jan 03 '17 at 16:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132217/discussion-between-ojf-and-pille). –  Jan 03 '17 at 16:21

3 Answers3

6

I had the same issue, because I didn't added

module.exports = router; 

in the bottom of my routes/index.js, make sure you are export as a router

Just Fabio
  • 61
  • 1
  • 1
4

Here you need to correct the path.

app.use('/api/elasticsearch', require('../elasticsearch/index.js'));
Satyam Koyani
  • 4,236
  • 2
  • 22
  • 48
  • That solves my problem, thank you! But the question remain why others routes do behave as expected and only this one does not... – Pille Jan 03 '17 at 16:53
0
import {body, validationResult} = require('express-validator');

So, If you are using express-validator then use body() instead of check().

Example:-

Use,

body('email').isEmail();

instead of,

check('email').isEmail();

Here email is your field name.

And check you are exporting router or not.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129