Share On Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit
Question

TypeError: Router.use() requires a middleware function but got a Object

Tags: router middleware node.js
Date:
Status:Resolved
Question Id:16

I have an error in my node.js project. I think it is about routers and middlewares.

app.js

var indexRouter = require('./routes/index');
var reportRouterrequire('./routes/report');
var pdfRouter = require('./routes/pdf');
var alisRouterrequire('./routes/alis');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname'public')));
app.use('/pdf'pdfRouter);
app.use('/'indexRouter);
app.use('/report'reportRouter);
app.use('/alis'alisRouter);

I took the error when I add alis router.

alis.js

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

require('tls').DEFAULT_MIN_VERSION = 'TLSv1'

var url="https://lab.ism.gov.tr/Alisws2konak/alisws.asmx";
var request = require('request-promise');
var VENUSER"superneuro"
var VENPASS="superneuro"
var parser = require('fast-xml-parser');
var DOCTOR_CODE='';
var moment = require('moment');


router.get('/ALISEntegKodlari'async function(reqresnext) {
    var xml='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> <soapenv:Header/>';
     xml+='<soapenv:Body><tem:ALISEntegKodlari><!--Optional:--><tem:VENUSER>'+VENUSER+'</tem:VENUSER><!--Optional:--><tem:VENPASS>'+VENPASS+'</tem:VENPASS>';
     xml+='</tem:ALISEntegKodlari></soapenv:Body></soapenv:Envelope>';
  
  
    var hdr = {
      'content-type': 'text/xml;charset=UTF-8',
      'SOAPAction': "http://tempuri.org/ALISEntegKodlari",
    };
  
    var opt = {
        method: 'post',
        headers: hdr,
        body:xml,
        host:"lab.ism.gov.tr",
        Connection:'Keep-Alive'
    };
    var parsingOptions = {};
    body=await request(url,opt)
    var jsonResult = parser.parse(bodyparsingOptions); //xml to json
    var entegreler=jsonResult['soap:Envelope']['soap:Body']['ALISEntegKodlariResponse']['ALISEntegKodlariResult']['EntegKodArr']['EntegKodlar'];
     res.json({entegreler:entegreler});
});
 

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

Answer
Date:
Correct:No

If your are using express above 2.x, you have to declare app.router like below code. Please try to replace your code

app.use('/', routes);

with

app.use(app.router);
routes.initialize(app);

Please click here to get more details about app.router

Note:

app.router is depreciated in express 3.0+. If you are using express 3.0+, refer to Anirudh's answer below.

Answer
Date:
Correct:Yes
Simple solution if your are using express and doing

const router = express.Router();
make sure to

module.exports = router ;
at the end of your page
Answer
Date:
Correct:No

thank you for correct answer.

Your Answer

Review Your Answer