admin管理员组

文章数量:1023282

hey guys m using to files for server side server.js and at client side using javascript

server.js

var express = require("express");
var io = require("socket.io").listen(app);
var app = express.createServer("localhost");
app.listen(3000);


app.get('/', function (req, res) {
    res.sendfile(__dirname + '/hello.html');
});

    console.log("before");
    io.sockets.on("connection", function (socket) {
        console.log("after");
        socket.on("message send", function (data) {
            io.sockets.emit("new mesage", data);
        });
    });

and at client side using one html page name

hello.html

<html>
<head>
    <style>
        #chat{
        height : 500px;
        }

    </style>

</head>
<body>
    <div id="chat" > </div>
    <form id ="send message"> 
        <input id="message" type="text" ></input>
        <input type="submit"></input>
    </form>
    <script type= "javascript" src=".min.js"></script>
    <script src="/socket.io/socket.io.js"></script>

    <script>
    jQuery(function($){
        var socket =io.connect();
        var $messageForm = $("#send message");
        var $messageBox=$("#message");
        var $chat = $("#chat");

            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit("message send" , $messageBox.val());
            });
            socket.on("new message", function(data){
                $chat.append(data + "</br>");   
            });
    });
    </script>

</body>
</html>

when m start server then its proporly working but when i sent a request then it shows error like given blow

Failed to load resource: the server responded with a status of 404 (Not Found) /socket.io/socket.io.js

Uncaught ReferenceError: jQuery is not defined

versions

socket.io:0.9.16

exparess:3.4.0

thanks for your time :)

hey guys m using to files for server side server.js and at client side using javascript

server.js

var express = require("express");
var io = require("socket.io").listen(app);
var app = express.createServer("localhost");
app.listen(3000);


app.get('/', function (req, res) {
    res.sendfile(__dirname + '/hello.html');
});

    console.log("before");
    io.sockets.on("connection", function (socket) {
        console.log("after");
        socket.on("message send", function (data) {
            io.sockets.emit("new mesage", data);
        });
    });

and at client side using one html page name

hello.html

<html>
<head>
    <style>
        #chat{
        height : 500px;
        }

    </style>

</head>
<body>
    <div id="chat" > </div>
    <form id ="send message"> 
        <input id="message" type="text" ></input>
        <input type="submit"></input>
    </form>
    <script type= "javascript" src="http://code.jquery./jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>

    <script>
    jQuery(function($){
        var socket =io.connect();
        var $messageForm = $("#send message");
        var $messageBox=$("#message");
        var $chat = $("#chat");

            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit("message send" , $messageBox.val());
            });
            socket.on("new message", function(data){
                $chat.append(data + "</br>");   
            });
    });
    </script>

</body>
</html>

when m start server then its proporly working but when i sent a request then it shows error like given blow

Failed to load resource: the server responded with a status of 404 (Not Found) /socket.io/socket.io.js

Uncaught ReferenceError: jQuery is not defined

versions

socket.io:0.9.16

exparess:3.4.0

thanks for your time :)

Share Improve this question edited Jul 13, 2017 at 11:40 Binit Ghetiya asked Oct 11, 2013 at 10:20 Binit GhetiyaBinit Ghetiya 1,9892 gold badges21 silver badges32 bronze badges 2
  • Can you mention which version of express and soclet.io you are using...? – Sriharsha Commented Oct 11, 2013 at 10:57
  • version socket.io = 0.9.16 exparess 3.4.0 – Binit Ghetiya Commented Oct 11, 2013 at 12:12
Add a ment  | 

1 Answer 1

Reset to default 3

You need to bind the socket.io to the http server not the application server. Changes this to below..

var express = require('express'),
    app = express(),
    http = require('http'),
    server = http.createServer(app),
    io = require('socket.io').listen(server);

Refer Express guide for more info.

EDIT

To correct the Jquery error..

replace

jQuery(function($){

with

$(document).ready(function($)

hey guys m using to files for server side server.js and at client side using javascript

server.js

var express = require("express");
var io = require("socket.io").listen(app);
var app = express.createServer("localhost");
app.listen(3000);


app.get('/', function (req, res) {
    res.sendfile(__dirname + '/hello.html');
});

    console.log("before");
    io.sockets.on("connection", function (socket) {
        console.log("after");
        socket.on("message send", function (data) {
            io.sockets.emit("new mesage", data);
        });
    });

and at client side using one html page name

hello.html

<html>
<head>
    <style>
        #chat{
        height : 500px;
        }

    </style>

</head>
<body>
    <div id="chat" > </div>
    <form id ="send message"> 
        <input id="message" type="text" ></input>
        <input type="submit"></input>
    </form>
    <script type= "javascript" src=".min.js"></script>
    <script src="/socket.io/socket.io.js"></script>

    <script>
    jQuery(function($){
        var socket =io.connect();
        var $messageForm = $("#send message");
        var $messageBox=$("#message");
        var $chat = $("#chat");

            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit("message send" , $messageBox.val());
            });
            socket.on("new message", function(data){
                $chat.append(data + "</br>");   
            });
    });
    </script>

</body>
</html>

when m start server then its proporly working but when i sent a request then it shows error like given blow

Failed to load resource: the server responded with a status of 404 (Not Found) /socket.io/socket.io.js

Uncaught ReferenceError: jQuery is not defined

versions

socket.io:0.9.16

exparess:3.4.0

thanks for your time :)

hey guys m using to files for server side server.js and at client side using javascript

server.js

var express = require("express");
var io = require("socket.io").listen(app);
var app = express.createServer("localhost");
app.listen(3000);


app.get('/', function (req, res) {
    res.sendfile(__dirname + '/hello.html');
});

    console.log("before");
    io.sockets.on("connection", function (socket) {
        console.log("after");
        socket.on("message send", function (data) {
            io.sockets.emit("new mesage", data);
        });
    });

and at client side using one html page name

hello.html

<html>
<head>
    <style>
        #chat{
        height : 500px;
        }

    </style>

</head>
<body>
    <div id="chat" > </div>
    <form id ="send message"> 
        <input id="message" type="text" ></input>
        <input type="submit"></input>
    </form>
    <script type= "javascript" src="http://code.jquery./jquery-latest.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>

    <script>
    jQuery(function($){
        var socket =io.connect();
        var $messageForm = $("#send message");
        var $messageBox=$("#message");
        var $chat = $("#chat");

            $messageForm.submit(function(e){
                e.preventDefault();
                socket.emit("message send" , $messageBox.val());
            });
            socket.on("new message", function(data){
                $chat.append(data + "</br>");   
            });
    });
    </script>

</body>
</html>

when m start server then its proporly working but when i sent a request then it shows error like given blow

Failed to load resource: the server responded with a status of 404 (Not Found) /socket.io/socket.io.js

Uncaught ReferenceError: jQuery is not defined

versions

socket.io:0.9.16

exparess:3.4.0

thanks for your time :)

Share Improve this question edited Jul 13, 2017 at 11:40 Binit Ghetiya asked Oct 11, 2013 at 10:20 Binit GhetiyaBinit Ghetiya 1,9892 gold badges21 silver badges32 bronze badges 2
  • Can you mention which version of express and soclet.io you are using...? – Sriharsha Commented Oct 11, 2013 at 10:57
  • version socket.io = 0.9.16 exparess 3.4.0 – Binit Ghetiya Commented Oct 11, 2013 at 12:12
Add a ment  | 

1 Answer 1

Reset to default 3

You need to bind the socket.io to the http server not the application server. Changes this to below..

var express = require('express'),
    app = express(),
    http = require('http'),
    server = http.createServer(app),
    io = require('socket.io').listen(server);

Refer Express guide for more info.

EDIT

To correct the Jquery error..

replace

jQuery(function($){

with

$(document).ready(function($)

本文标签: javascriptSocket Failed to load resource server (404 error)Stack Overflow