Complete the code to import the WebSocket library.
const WebSocket = require('[1]');
The ws library is used to create WebSocket servers in Express apps.
Complete the code to create a WebSocket server attached to an existing HTTP server.
const wss = new WebSocket.Server({ [1]: server });The server option attaches the WebSocket server to the existing HTTP server.
Fix the error in extracting the token from the WebSocket upgrade request headers.
const token = req.headers['[1]'];
The authorization header commonly carries the token for authentication.
Fill both blanks to verify the token and accept or reject the WebSocket connection.
wss.on('connection', (ws, req) => { const token = req.headers['authorization']; if () { ws.[2](); return; } // connection accepted });
verifyToken checks if the token is valid. If not, ws.close() closes the connection.
Fill all three blanks to extract the token, verify it asynchronously, and accept or reject the connection.
wss.on('connection', async (ws, req) => { const token = req.headers['[1]']; try { const user = await [2](token); if (!user) { ws.[3](); return; } // connection accepted with user info } catch { ws.close(); } });
The token is in the authorization header. verifyTokenAsync is an async function to verify it. If invalid, ws.close() rejects the connection.