# chatRoom **Repository Path**: geekcjj/chatRoom ## Basic Information - **Project Name**: chatRoom - **Description**: 使用socket.io实现的一个实时多人聊天 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2019-04-26 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README problem1:You are trying to attach socket.io to an express request handler function. Please pass a http.Server instance. solution: https://stackoverflow.com/questions/25000275/socket-io-error-hooking-into-express-js var http = require('http'); var express = require('express'), app = module.exports.app = express(); var server = http.createServer(app); var io = require('socket.io').listen(server); //pass a http.Server instance server.listen(80); //listen on port 80 problem2: io.on('connection',function(socket){});--------io.on is not a function solution: var socket = io.listen(server); socket.on('connection', function (socket) { socket.on('message', function (data) { socket.send(data); }) }); problem3: express 获取请求体req.body出现空对象 let data = { "name": document.getElementById("name").value, "password": document.getElementById("password").value } console.log(data); let xhr = new XMLHttpRequest(); xhr.open("POST","http://localhost:3000/userRegister",true); xhr.onreadystatechange = function () { if(xhr.readyState === 4 && xhr.status === 200) { alert("注册成功"); } else { alert("注册失败"); } } xhr.send(JSON.stringify(data)); solution:关键:xhr.setRequestHeader("Content-type","application/json");//缺少则req.body为空 xhr.send(JSON.stringify(data)); 没有json.stringify()则bad request