首页 > 解决方案 > 节点JS | 在服务器中使用两个协议

问题描述

我想在我的服务器上使用 http 协议和 websocket,我将解释:

我有一个聊天应用程序,我有一个登录页面和聊天页面。我想在登录页面上使用 http 协议,在聊天页面上使用 websocket。

如果答案是肯定的,我可以这样做吗?

const express = require('express')
const http = require('http')

const app = express()

const server = http.createServer(app)
const io = socketio(server)

in.on('connection', (req,res) => {
  //...
})

app.get('/',(req,res) => {
  //...
})

server.listen(port, (req, res) => {
  console.log('Server is listen on port ' + port)
})

标签: javascriptnode.js

解决方案


推荐阅读