Middleware example is outdated, can't get it working anymore
See original GitHub issueVersion: 6.0.8
Environment:
- Operating system: Windows 10
- Browser: Google Chrome
- Node.js: 7.3.0
- Transformer: uws
Expected result: Establish a connection between server and client Able to set and read session object shared between Express and Primus
Actual result: If I set
primusSession.upgrade = true // or not set it at all
I can read and set the session object at spark.request.session
as expected. However, I get the following error in my browser when trying to connect
WebSocket connection to 'ws://localhost:3000/gameserver?_primuscb=LcrjIwC' failed: Connection closed before receiving a handshake response
opening @ primus.js:3212
emit @ primus.js:300
reconnect @ primus.js:3246
emit @ primus.js:279
reconnect @ primus.js:2341
emitter @ primus.js:146
emit @ primus.js:1007
delay @ primus.js:849
tickedtock @ primus.js:1316
If I set
primusSession.upgrade = false
The browser connects to the client successfullly however the middleware doesn’t get called at all, and I can’t set or read sessions in Primus.
Steps to reproduce:
I’ll try to set up a clean project to reproduce the issue, but here are code snippets for now.
primus-session.js
/**
* Slightly modified version of this middleware example
* Notably, all references to cookie-parser have been rewritten since the latest express-session module doesn't use it anymore
* https://github.com/primus/primus/blob/master/examples/middleware/session.js
*/
'use strict'
//
// Expose the configuration function.
//
module.exports = function configure(options) {
const store = options.store
const primus = this
if (!store) {
//
// Throw an error when the session store is not passed.
//
const message = 'Session middleware configuration failed due to missing '
+ '`store` option'
throw new Error(message)
}
//
// The actual session middleware. This middleware is async so we need 3
// arguments.
//
function session(req, res, next) {
//
// The session id is stored in the cookie
//
const sid = req.headers.cookie
console.log('sid:', sid)
//
// Default to an empty session.
//
req.session = {}
//
// If we don't have a session id we are done.
//
if (!sid) return next()
//
// Grab the session from the store.
//
store.get(sid, function (err, session) {
//
// We don't want to kill the connection when we get an error from the
// session store so we just log the error.
//
if (err) {
primus.emit('log', 'error', err)
return next()
}
if (session) req.session = session
next()
})
}
// Don't call this middleware in an Upgrade request
session.upgrade = false
session.http = true
return session
}
Express configuration
// Sessions are shared between Express and Primus
const store = new expressSession.MemoryStore()
const session = expressSession({
secret: 'change in production',
resave: true,
saveUninitialized: true,
store,
})
// Express settings and middleware
app.set('port', process.env.PORT || 3000)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use(logger('dev'))
app.use(express.static(path.join(__dirname, 'public')))
app.use(session)
Primus configuration
const primus = new Primus(server, {
pathname: '/gameserver',
transformer: 'uws',
})
// Primus middleware
primus.use('session', primusSession, { store })
primus.use((req, res) => {
console.log(req.session)
})
Issue Analytics
- State:
- Created 7 years ago
- Comments:19 (11 by maintainers)
Top Results From Across the Web
Middleware not working for 400 BadRequest #67 - GitHub
when BadRequest() without a string is used, it returns a ProblemDetail object. The moment you sue an "explainer" text it doesn't work anymore.....
Read more >Why is Custom Middleware Still Being Run? - Stack Overflow
The middleware seems to be cached or something. I don't understand it. When I request localhost:6000/url-that-should-work , I'm magically ...
Read more >Middleware Is Outdated & Why You SHOULDN'T Be Using It
We'll explain what middleware is, why it is outdated and why you shouldn't use it. There are agile and scalable integration solutions to ......
Read more >Abusing Express Middleware - Medium
The old middleware can then be deleted when the header is not used anymore. If middleware overrides values which have already been set...
Read more >Top 8 Middleware Software Platforms in 2021 | Spiceworks
Learn which middleware software platforms to use in 2021 to strengthen your enterprise ... Key Must-Have Features for Middleware Software.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I meant that the middleware example in
/examples
runs without any modification (usingwebsockets
as default transformer).Hmm, that’s surprising. Feel free to close the issue, I’ll create another one at
uws
. Thanks for all the help!@prashcr sockjs is “special” as it overrides the
session
property. If you change the Primus example like this:it works and that’s another reason why
expression-session
shouldn’t be used directly.So the Primus example works for you on Windows when using uws?