indutny / sticky-session

Sticky session balancer based on a `cluster` module
964 stars 99 forks source link

TypeError: sticky is not a function #58

Open kingthrillgore opened 6 years ago

kingthrillgore commented 6 years ago

I know this library isn't getting much support, but I am seeing a recurring issue with the latest npm version (1.1.2) and Express (4.16.2) and a TypeError that goes against an example of using stickysession as specified for Express:

var express = require('express');
var http    = require('http');
var sticky  = require('sticky-session');
const APP_PORT = 8080;

var Server = express();
var HttpServer = http.createServer(Server);

sticky(HttpServer).listen(APP_PORT, function() {
   console.log("Server listening at port %d", APP_PORT);
});
/Users/ck/StickySessionTest/server.js:9
sticky(HttpServer).listen(APP_PORT, function() {
^

TypeError: sticky is not a function
    at Object.<anonymous> (/Users/cameron/StickySessionTest/server.js:9:1)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Function.Module.runMain (module.js:682:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:613:3
Valentinishe commented 6 years ago

sticky really isn't a function, you need to use

if (!sticky.listen(HttpServer, APP_PORT)) {
  HttpServer.once('listening', function () {
    console.log("Server listening at port %d", APP_PORT);
  });
}
Valentinishe commented 6 years ago

And hint for you - try to use websockets instead of http-polling, and you will not need sticky.