简体   繁体   English

nodejs-找不到socket.io

[英]nodejs - socket.io not found

I'm not sure why the file isn't found. 我不确定为什么找不到该文件。 From the documentation I read, socket.io should be exposing itself automatically. 根据我阅读的文档,socket.io应该会自动公开。

The error: 错误:

polling-xhr.js?bd56:264 GET http://localhost:8081/socket.io/?EIO=3&transport=polling&t=LyYgQtO 404 (Not Found)

Here's my code: 这是我的代码:

Server: 服务器:

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')
})

Front end: 前端:

<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>

Ah, seems like I just needed to make another express thing, here's the updated server: 嗯,好像我只需要做另外一件快递事情,这是更新的服务器:

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
// app.use(cors())

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 appSocket = express()
var http = require('http').Server(appSocket);
var io = require('socket.io')(http);
// app.options('*', cors())

http.listen(8082)

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')
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM