描述:实现一个简单的聊天室功能,可以实时的接收到其他user发送的信息,可以查看源码,效果如下图:
涉及到的技术:
back end: express socket.io sequelize jasmine
front end: bootstrap
实现的关键点:
- 设置好监听端口,浏览器localhost:3000访问时,返回404,添加index.html入口页面
app.use(express.static(__dirname))
- post请求,req.body={}, 需要parse application/json
app.use(bodyParser.json())
- parse application/x-www-form-urlencoded, 如果没有解析对应的Content-Type,req.body=undefined
app.use(bodyParser.urlencoded({extended: false}))
- socket.io的使用
server: const server = require('http').createServer(app); const io = require('socket.io')(server) io.on('connection', () => {}) io.emit('xx', obj) client: <script src="/node_modules/socket.io/client-dist/socket.io.min.js"></script> const socket = io.connect('http://localhost:4000/'); socket.on('xx', () => {})
- sequelize,mysql2数据存储
- 异步流程控制: promise/async await
- jasmine for node / test-driven development
npm install jasmine --save-dev npm install request --save-dev (数据请求) npx jasmine init