socket.io & express: 404 not found

385
April 30, 2017, at 03:43 AM

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

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

Any idea? Thanks

Answer 1

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.

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

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

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.

READ ALSO
Get a reference to the event listener from a middleware

Get a reference to the event listener from a middleware

Is there a way to get a reference to the event listener as in the following stub?

202
Using ws and wss simultaneously

Using ws and wss simultaneously

I am trying to get both ws and wss working, on one WebSocket server

405
Messenger Chatbot Error validating access token: The user is enrolled in a blocking, logged-in checkpoint

Messenger Chatbot Error validating access token: The user is enrolled in a blocking, logged-in checkpoint

I recently submitted a messenger chatbot for reviewFew minutes after the submission, I couldn't deploy new changes from github anymore

951