简体   繁体   中英

Issues using node with socketio

I am stuck i using nodejs with socket IO.

I am using windows machine, downloaded Node executable , set the environment variables properly.

This way i was able to run a simple node server on command.

Then i used npm install socketio even i got the socketio installed(path as c:\\Aakash\\Users\\nodemodules....)

Now when i am using a sample code for running node server equiped with socket, it throws error in socketio js saying that "indexOf at line 1324 is not defined".

This errow comes at the line in the server.js where i have written

require("socket.io");

May be It is not able to execute socket.io.

Confusions : 1) The socketio files are somewhere in Users\\nodemodules... how will it fetch the socketio in the require statement???

2) similarly on the client side i require socket.io.js, that is too inside that Users.. directory, but here i copied the socket.io.js and put it near the clientfile.html.

Is i am doing wrong in the directory structure of socketio? Where and what files should be included near the Client files?

I searched for the above questions, but was not succesful.

Thanks.

First of all

Answere 1 :

1.create your project folder(any where)

2.Navigate your project director in cmd

3.Install socket.io (npm install socket.io)

After install socket.io You will found node_module directory in your project folder.

Answere 2 :

Now your client will fetch socket.io file (Just include src='/socket.io/socket.io.js')

Did you use some thing like this:

If you use http module then

var app = require('http').createServer(handler)
  , 
io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(1324);

function handler (req, res) {
   // your handler
}

I think You should use express module for further detail for socket.io

If you install a module global (npm install module -g) and then you will try to install it on your project's folder(without -g), it will not install again because it is already installed. But if don't have it installed globally, you can install it as many times as you want, in multiple folders. For more information about how node loads modules, you can read this and this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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