0
파일을 찾을 수없는 이유가 확실하지 않습니다. 읽은 문서에서 socket.io가 자동으로 노출되어야합니다.nodejs - socket.io 찾을 수 없음
오류 : 여기
polling-xhr.js?bd56:264 GET http://localhost:8081/socket.io/?EIO=3&transport=polling&t=LyYgQtO 404 (Not Found)
내 코드입니다 :
서버 :
const cors = require('cors')
var express = require('express')
var app = express()
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
var whitelist = ['http://localhost', 'http://localhost:8080', 'http://127.0.0.1:8080', 'http://127.0.0.1']
var corsOptions = {
credentials: true,
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
app.use(cors(corsOptions))
var http = require('http').Server(app);
var io = require('socket.io')(http);
// app.options('*', cors())
var db = require('./db.js')
io.on('connection', function(socket){
console.log('a user connected');
});
app.post('/', cors(corsOptions), (req, res) => {
res.send("post test\n" + req.body.test)
})
app.get('/', cors(corsOptions), (req, res) => {
res.send("works\n")
})
app.listen(8081,() => {
console.log('API listening on port 8081')
})
프런트 엔드 : 그냥 만드는 데 필요한 같은
<template src="./templates/Test.html"></template>
<script>
import io from 'socket.io-client'
export default {
name: "test",
mounted: function() {
io.connect("http://localhost:8081")
}
}
</script>