Error - expressSessionMiddleware is not a function

tom894

New Member
#1
I'm trying to use express-socket.io-sessions to sockets with the users' sessions, but I keep getting this error when I try add the middleware. Here is my code:
Mã:
var express = require('express');
var socket = require('socket.io');
var sharedSession = require("express-socket.io-session");

function main(req, res, server, session) {

    res.sendFile(__dirname + "/views/find.html");

    var io = socket(server);

    io.use(sharedSession(session, {
        autoSave: true
    }));

}
All the parameters passed to the main function seemed when I console.log'd them, so I don't think it's that. Any help on why this error occurs would be much appreciated. Thanks.
 

Admin

Administrator
Thành viên BQT
#2
You also have to middleware session into express app after that you can use the sharedSessionmiddleware.

Correct way

Mã:
var app = require('express')(),
  server  = require("http").createServer(app),
  io = require("socket.io")(server),
  session = require("express-session")({
    secret: "my-secret",
    resave: true,
    saveUninitialized: true
  }),
  sharedsession = require("express-socket.io-session");


// Attach session
app.use(session);

// Share session with io sockets

io.use(sharedsession(session));
Reference to official documentation - https://www.npmjs.com/package/express-socket.io-session
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top