最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Express + socket.io: socket.io client script is 404 - Stack Overflow

programmeradmin2浏览0评论

This is driving me crazy... while I have a working version of Express + Socket.io, I can't seem to reproduce it with out-of-the-box NPM installs in a new project folder. Can anyone point out what I'm missing...? Here's my process:

I create a node_modules folder in my project directory (pwd), then do:

npm install express
npm install socket.io

Running those two mands puts the packages in my project's node_modules folder as expected. Now I set up my server with the following:

var express = require('express'),
    server = express.createServer().use( express.static(__dirname+'./public') ).listen( 8080 ),
    io = require('socket.io').listen(server);

My public folder contains static assets for my application. My public index HTML page includes a script tag for:

<script src="/socket.io/socket.io.js"></script>

Finally, I run my server script and go to the application in a web browser. My static public files are all served properly, however I get a 404 for /socket.io/socket.io.js. Now, I can swap in an express package from another old project and have this whole system work. Somehow that package instance is configured differently, but I can't figure out how to reproduce that. The Express website mentions something about installing dependencies, although running npm install -d doesn't seem to help (is there a specific pwd that I need to be in while running npm install -d?). I figure I must be missing something important about configuring a new Express instance after installing it with NPM.

Thanks for any and all insight!

This is driving me crazy... while I have a working version of Express + Socket.io, I can't seem to reproduce it with out-of-the-box NPM installs in a new project folder. Can anyone point out what I'm missing...? Here's my process:

I create a node_modules folder in my project directory (pwd), then do:

npm install express
npm install socket.io

Running those two mands puts the packages in my project's node_modules folder as expected. Now I set up my server with the following:

var express = require('express'),
    server = express.createServer().use( express.static(__dirname+'./public') ).listen( 8080 ),
    io = require('socket.io').listen(server);

My public folder contains static assets for my application. My public index HTML page includes a script tag for:

<script src="/socket.io/socket.io.js"></script>

Finally, I run my server script and go to the application in a web browser. My static public files are all served properly, however I get a 404 for /socket.io/socket.io.js. Now, I can swap in an express package from another old project and have this whole system work. Somehow that package instance is configured differently, but I can't figure out how to reproduce that. The Express website mentions something about installing dependencies, although running npm install -d doesn't seem to help (is there a specific pwd that I need to be in while running npm install -d?). I figure I must be missing something important about configuring a new Express instance after installing it with NPM.

Thanks for any and all insight!

Share Improve this question asked Jun 27, 2012 at 19:08 bigmacbigmac 7178 silver badges24 bronze badges 3
  • 1 What version of express and socket.io is installing? I just went through the steps you did, and both static assets and socket.io.js is serving fine. – Timothy Strimple Commented Jun 27, 2012 at 19:32
  • 1 the client side socket.io.js is generated dynamically, it shouldn't have anything to do with your express static server. Can I get more information about the route that is failing? Check to what happens if you go to localhost:8000/socket.io/socket.io.js or localhost:8000/socket.io (replace 8000 with the port number you are running your app on) – Sdedelbrock Commented Jun 27, 2012 at 19:52
  • Ditto what Timothy said...it works on my machine. Although in Windows I had to change the path to the static files as follow: __dirname+'/public' (removed the dot) – Hector Correa Commented Jun 27, 2012 at 20:02
Add a ment  | 

1 Answer 1

Reset to default 7

Okay, so my example was actually an abbreviation of my code, and that example code does actually work. My real code with problems was a bit more cluttered, like so:

var server = express.createServer();

server
    .use( server.router )
    .use( express.static(__dirname+'/../public') )
    .get('/api', function(req, res) {
        res.write('API');
    });

server.listen(8080);

var io = require('socket.io').listen(server);

I fixed the above code by doing the following:

server = server.listen(8080);

Apparently the listen mand wraps the server object with some additional functionality. My originally posted shorthand example actually does work because listen is chained onto the final return into the server variable. Interesting little nuance.

发布评论

评论列表(0)

  1. 暂无评论