node-run-middleware icon indicating copy to clipboard operation
node-run-middleware copied to clipboard

TypeError: Cannot read property 'pipesCount' of undefined

Open bwyss opened this issue 8 years ago • 11 comments

I am trying to use run-middleware within an angular-fullstack application.

My application is using express for managing sessions and passport.js for authentication (stored in Mongodb). When a user logins in, I want to check if that user already has a living session. I want to use run-middleware to programmatically query mongodb for all the live sessions.

'use strict';

import path from 'path';

import passport from 'passport';
import {Strategy as LocalStrategy} from 'passport-local';


import express from 'express';
import session from 'express-session';


import _ from 'lodash';
import Session from '../../api/session/session.model';


var app = express();

require('run-middleware')(app);


function localAuthenticate(User, email, password, done, req) {
  User.findOne({
    email: email.toLowerCase()
  }).exec()
    .then(user => {

      if (!user) {
        return done(null, false, {
          message: 'This email is not registered.'
        });
      }

      // HERE is where I am trying to use the runMiddleware 
      app.runMiddleware('/sessions',{},function(code,data){
        console.log(code) // 200 
        console.log(data) // { user: '20', name: 'Moyshale' }
      });

      user.authenticate(password, function(authError, authenticated) {
        if (authError) {
          return done(authError);
        }
        if (!authenticated) {
          return done(null, false, { message: 'This password is not correct.' });
        } else {
          return done(null, user);
        }
      });
    })
    .catch(err => done(err));
}

export function setup(User, config) {

  passport.use(new LocalStrategy({
    passReqToCallback: true,
    usernameField: 'email',
    passwordField: 'password' // this is the virtual field on the model
  }, function(req, email, password, done) {
    return localAuthenticate(User, email, password, done, req);
  }));
}

The error I am getting is:

if (state.pipesCount === 0)
           ^
TypeError: Cannot read property 'pipesCount' of undefined
    at IncomingMessage.Readable.unpipe (_stream_readable.js:634:12)
    at unpipe (/home/enview/node_modules/unpipe/index.js:47:12)
    at send (/home/enview/node_modules/finalhandler/index.js:184:3)
    at Immediate.<anonymous> (/home/enview/node_modules/finalhandler/index.js:113:5)
    at Immediate.<anonymous> (/home/enview/node_modules/express/lib/router/index.js:618:15)
    at runCallback (timers.js:568:20)
    at tryOnImmediate (timers.js:546:5)
    at processImmediate [as _immediateCallback] (timers.js:525:5)


bwyss avatar Dec 20 '16 19:12 bwyss

Looks like adding any sort of middleware is enough to break it :(

var express = require('express');
var runMiddleware = require('run-middleware');

var app = express();

app.use(function (req, res, next) {
    console.log({uri: req.originalUrl, method: req.method, ip: req.ip}, 'Request');
    next();
});

app.get('/foo', function (req, res) {
    res.json({result: 'OK'});
});

runMiddleware(app);

app.runMiddleware('/foo', function (code, body) {
    console.log('CODE:', code);
    console.log('BODY:', body);
});

results in

_stream_readable.js:633
  if (state.pipesCount === 0)
           ^

TypeError: Cannot read property 'pipesCount' of undefined
    at IncomingMessage.Readable.unpipe (_stream_readable.js:633:12)
    at unpipe (/home/borisov/test/node-run-middleware/node_modules/unpipe/index.js:47:12)
    at send (/home/borisov/test/node-run-middleware/node_modules/finalhandler/index.js:275:3)
    at Immediate.<anonymous> (/home/borisov/test/node-run-middleware/node_modules/finalhandler/index.js:135:5)
    at Immediate.<anonymous> (/home/borisov/test/node-run-middleware/node_modules/express/lib/router/index.js:635:15)
    at runCallback (timers.js:674:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)

borisovg avatar Aug 24 '17 13:08 borisovg

Anyone have any idea about what to do if this happens?

azaslavsky avatar Apr 10 '18 19:04 azaslavsky

Just ran into this as well.

deejayres avatar Apr 13 '18 16:04 deejayres

I'm getting this error from an entirely different setup that doesn't even include node-run-middleware. So maybe the problem is deeper - like Express itself.

alflennik avatar Apr 24 '18 20:04 alflennik

I ran into this same issue. Solved by passing the request parameter {connection: {}}. E.g.:

app.runMiddleware('/handler', {connection: {}}, function(code, data) {
  console.log(code, data);
  process.exit();
})

netpedro-com avatar Apr 29 '18 02:04 netpedro-com

I solved it by passing original_req parameter:

app.runMiddleware('/another-route', {original_req: req}, function(err, data) {
  ...
});

WeishengChang avatar Dec 22 '18 02:12 WeishengChang

this still happens if you have routes configured with sendFile:

app.get(CLIENT_ROUTES, function(req, res) {
  res.sendFile(join(DIST_FOLDER, 'browser', 'index.html'));
});

I tried every solution proposed above to no avail.

therealpecus avatar Nov 21 '19 15:11 therealpecus

I solved it by passing original_req parameter:

app.runMiddleware('/another-route', {original_req: req}, function(err, data) {
  ...
});

this worked for me

hotlib avatar May 26 '20 15:05 hotlib

Try to delete the response.json() of the api /sessions , it works for me.

danielponce263 avatar Jan 29 '21 05:01 danielponce263

If anyone wants to create a PR to fix it, I will merge it

Aminadav avatar Mar 13 '21 23:03 Aminadav

I had this issue before I had a catchall route set up to 404. Basically when a url was accessed where no route matched then this happened. Adding a catchall after all other routes were registered (so it only runs if nothing else matched) fixed it in my case. app.all('*', ...

dustinbolton avatar Nov 20 '23 18:11 dustinbolton