套接字。io & express: 404未找到

时间:2022-08-22 16:27:59

My app.js

我app.js

const express = require('express'),
      morgan = require('morgan'),
      bodyParser = require('body-parser'),
      path = require('path'),
      mongoose = require('mongoose'),
      app = express(),
      config = require('./config'),
      Note = require('./models/note'),
      server = require('http').createServer(app),
      io = require('socket.io')(server),
      socket = io.socket;

mongoose.connect('mongodb://'+config.db.host+'/'+config.db.name);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Serve static assets
app.use(express.static(path.resolve(__dirname, '..', 'build')));

app.use(function(req, res, next) {
  const allowedOrigins = [
    'http://127.0.0.1:8000',  
    'http://localhost:8000',  
    'http://127.0.0.1:3000',  
    'http://localhost:3000'];
  const origin = req.headers.origin;
  if(allowedOrigins.indexOf(origin) > -1){
    res.setHeader('Access-Control-Allow-Origin', origin);
  }
  //res.header("Access-Control-Allow-Origin", "127.0.0.1 localhost");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header('Access-Control-Allow-Credentials', true);
  next();
});

calling socket.emit() in handlers after above codes.

在上述代码之后的处理程序中调用socket.emit()。

My index.js

我index.js

'use strict';

const app = require('./app'),
      // server = http.createServer(app),
      PORT = process.env.PORT || 8000;

app.listen(PORT, () => {
  console.log(`REST API running on ${PORT}!`);
});

Console output:

控制台输出:

套接字。io & express: 404未找到

Any idea? Thanks

任何想法?谢谢

1 个解决方案

#1


3  

If you're going to do this:

如果你要这样做:

server = require('http').createServer(app),

Then, you can't do:

然后,你不能做的:

app.listen(PORT, ...);

because app.listen() will create a new and different server and socket.io will not be associated with that one.

因为app.listen()将创建一个新的不同的服务器和套接字。io不会与那个相关联。

Instead, you need to do:

相反,你需要做的是:

server.listen(PORT, ...)

using the server value from app.js. And, if you want to require() in the server from app.js, you also need to export it from app.js (something else I don't see your code doing).

使用app.js中的服务器值。而且,如果您想要在服务器中从app.js中要求(),还需要从app.js中导出(还有一些代码没有做的事情)。


For reference, the code for app.listen(), does this:

作为参考,app.listen()的代码是这样的:

app.listen = function listen() {
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

You can see how it creates a different server than the one you passed to socket.io. Thus the one you passed to socket.io is never started and thus socket.io does not work.

您可以看到它如何创建一个不同于您传递给socket.io的服务器。因此,您传递给socket的那个。io从未启动,因此是socket。io不工作。

#1


3  

If you're going to do this:

如果你要这样做:

server = require('http').createServer(app),

Then, you can't do:

然后,你不能做的:

app.listen(PORT, ...);

because app.listen() will create a new and different server and socket.io will not be associated with that one.

因为app.listen()将创建一个新的不同的服务器和套接字。io不会与那个相关联。

Instead, you need to do:

相反,你需要做的是:

server.listen(PORT, ...)

using the server value from app.js. And, if you want to require() in the server from app.js, you also need to export it from app.js (something else I don't see your code doing).

使用app.js中的服务器值。而且,如果您想要在服务器中从app.js中要求(),还需要从app.js中导出(还有一些代码没有做的事情)。


For reference, the code for app.listen(), does this:

作为参考,app.listen()的代码是这样的:

app.listen = function listen() {
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

You can see how it creates a different server than the one you passed to socket.io. Thus the one you passed to socket.io is never started and thus socket.io does not work.

您可以看到它如何创建一个不同于您传递给socket.io的服务器。因此,您传递给socket的那个。io从未启动,因此是socket。io不工作。