node-run-middleware
node-run-middleware copied to clipboard
TypeError: Cannot read property 'pipesCount' of undefined
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)
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)
Anyone have any idea about what to do if this happens?
Just ran into this as well.
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.
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();
})
I solved it by passing original_req
parameter:
app.runMiddleware('/another-route', {original_req: req}, function(err, data) {
...
});
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.
I solved it by passing
original_req
parameter:app.runMiddleware('/another-route', {original_req: req}, function(err, data) { ... });
this worked for me
Try to delete the response.json() of the api /sessions , it works for me.
If anyone wants to create a PR to fix it, I will merge it
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('*', ...