22.12.27 http와 app, socket.io

Han Lee·2022년 12월 27일
0

TIL

목록 보기
23/43

express 없이 서버 생성해서 실행 -> if문으로 url관리를 해줘야 하는 문제가 생김

const http = require("http")
const app = http.createServer((req,res) => {
	if(req.url === '/'){
    res.end('여기는 루트')
    }else if (req.url === "/login"){
    res.end('여기는 로그인')
    }
})
app.listen(3000, () => {
	console.log("http로 가동된 서버")
})

http에 express를 담아서 서버 실행

const express = require("express")
const http = require('http')
const app = express()
const server = http.createServer(app)

server.listen(3000, () => console.log('서버실행')

socket.io에도 server를 담아야한다.

const socketIo = require('socket.io')
const io = soketIo(server)

io.on("connection",(socket) => {
	console.log('연결')
})

socket.emit("연결할 소켓이름", 전할 내용)
socket.on("연결한 소켓이름", (받을 데이터) => {실행할 내용})

profile
렌덤형 인간

0개의 댓글