Node http2 - cannot read this.socket.readable
Problem
Testing node's new http2 native module I couldn't get express to serve requests over http2, Using node master build with `--expose-http2` flag: [code block] When requesting /express the server crashed with the following error: [code block]
Error Output
Error: Cannot read property 'readable' of undefined
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix http2 Socket Readable Error in Express
The error 'Cannot read property 'readable' of undefined' occurs because the http2 module in Node.js does not correctly initialize the socket object when using Express. This is likely due to the way the request and response objects are handled in the http2 context, which differs from the traditional http module. The Express framework may not be fully compatible with the http2 implementation without specific adjustments.
Awaiting Verification
Be the first to verify this fix
- 1
Install Required Packages
Ensure that you have the latest version of Express and http2 packages installed, as they may contain necessary updates for compatibility.
bashnpm install express http2 - 2
Modify Server Initialization
Change the way the Express app is initialized to use the http2.createServer method instead of the standard http.createServer. This ensures that the server is set up to handle HTTP/2 requests properly.
javascriptconst http2 = require('http2'); const express = require('express'); const app = express(); const server = http2.createServer(app); - 3
Handle HTTP/2 Specific Features
Ensure that you handle HTTP/2 specific features such as streams and headers correctly in your Express routes. For example, you may need to use the 'stream' object to send responses.
javascriptapp.get('/express', (req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello HTTP/2'); }); - 4
Start the Server
Start the server using the http2 server instance and ensure it listens on the desired port.
javascriptserver.listen(3000, () => { console.log('HTTP/2 server listening on port 3000'); }); - 5
Test the HTTP/2 Endpoint
Use a tool like curl with HTTP/2 support or a browser that supports HTTP/2 to test the /express endpoint and ensure it responds correctly without crashing.
bashcurl -i --http2 http://localhost:3000/express
Validation
Confirm that the server starts without errors and that requests to the /express endpoint return the expected response without crashing. Additionally, check that the response headers indicate HTTP/2 usage.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep