Node Express4.0+socket.io consolidated access to socket.io.js issues

Source: Internet
Author: User
Tags emit node server

Server.js Code


var express=require ("Express"),    Pipe=require ("./pipe.js"),    app=express (),    http=require (' http '). Createserver (APP)    sio=require (' Socket.io '). Listen (HTTP);//Do.......sio.on (' Connection ', function (socket) {   console.log (' connection ');   Socket.on (' msg ', function (a) {        console.log (a);        Sio.emit (' msg ', ' Your Sister ');})   ; App.listen (7777,function () {    Console.log ("app started Port is 7777");



After performing node Server.js run


Access/socket.io/socket.io.js to 404 on the browser

It took one hours to finally fix it.

It would be nice to change App.listen to Http.listen.


var express=require ("Express"),    Pipe=require ("./pipe.js"),    app=express (),    http=require (' http '). Createserver (APP)    sio=require (' Socket.io '). Listen (HTTP);//js merge pipe (app);//Shopping cart require ("./cart.js") (app); App.engine ("HTML", require ("./lib/tpl-node.js"));//require ('./test/websocket.js ') (app); app.gome={   static: "E : \\workspaces\\dev\\Storefront\\storedocroot.war ",   jsserver:" http://localhost:7777 ",   };app.use ( Express.static (app.gome.static)); App.use (Express.static ('./test ')); App.get ("/", function (req,res) {    res.send ("index"). end ();}); Sio.on (' Connection ', function (socket) {   console.log (' connection ');   Socket.on (' msg ', function (a) {        console.log (a);        Sio.emit (' msg ', ' Your Sister ');})   ; Http.listen (7777,function () {    Console.log ("app started Port is 7777");





The following is a complete server and client JS

Server.js


var express=require ("Express"),    Pipe=require ("./pipe.js"),    app=express (),    http=require (' http '). Createserver (APP)    sio=require (' Socket.io '). Listen (HTTP);//js merge pipe (app);//Shopping cart require ("./cart.js") (app); App.engine ("HTML", require ("./lib/tpl-node.js"));//require ('./test/websocket.js ') (app); app.gome={   static: "E : \\workspaces\\dev\\Storefront\\storedocroot.war ",   jsserver:" http://localhost:7777 ",   };app.use ( Express.static (app.gome.static)); App.use (Express.static ('./test ')); App.get ("/", function (req,res) {    res.send ("index"). end ();}); Sio.of ('/test '). On (' Connection ', function (socket) {   console.log (' connection ');   Socket.on (' msg ', function (a) {        console.log (a);        Socket.emit (' msg ', ' Your Sister ');})   ; Http.listen (7777,function () {    Console.log ("app started Port is 7777");



Client.js



<! DOCTYPE html>




Node Express4.0+socket.io consolidated access to socket.io.js issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.