简体   繁体   中英

Node.js and Jade

I'm pretty good with javascript but I am fairly new to node.js and jade. I'm attempting to create a basic chat server using socket.io (following a tutorial I found online) but I am having trouble getting it up and running.

To start off i created a basic package.json file that included the following which when run created node_modules directory within my working directory.

{
    "name": "Chat",
    "version": "1.0.0",
    "description": "Real Time Chat",
    "dependencies": {
         "socket.io": "latest",
        "express": "latest",
        "jade": "latest"
    },
    "author": "@pattmorter"
}

Good so far right?

Then I created the server.js file and the script.js file. Next I created the home.jade file for the UI.

doctype 5
html
    head
        title Chatter
        script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js')
        script(src='socket.io/socket.io.js')
        script(src='script.js')
    body
        //- some other formatting stuff that works correctly

When i start up my server and go to 127.0.0.1:3000, the page shows up but in the error console it says that GET http://127.0.0.1:3000/socket.io/socket.io.js 404 (Not Found) error.

I thought I referenced the file correctly but I guess not :( My thought process was that since the server.js file was rendering the home.jade file that I would only have to do script(src='socket.io/socket.io.js') within the jade file.

Any hint in the right direction would be greatly appreciated!

EDIT 1
Here is my server.js snippet

var express = require("express");
var app = express();
var jade = require('jade');
var io = require('socket.io').listen(app);

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set("view options", { layout: false });
app.configure(function() {
        app.use(express.static(__dirname + '/public'));
});
app.get('/', function(req, res){
  res.render('home.jade');
});
app.listen(3000);

io.sockets.on('connection', function (socket) {
    socket.on('setPseudo', function (data) {
        socket.set('pseudo', data);
    });
    socket.on('message', function (message) {
        socket.get('pseudo', function (error, name) {
            var data = { 'message' : message, pseudo : name };
            socket.broadcast.emit('message', data);
            console.log("user " + name + " send this : " + message);
        })
    });
});

Your socket.io initialization is incorrect.

Instead of this (which actually gives me a warning):

var io = require('socket.io').listen(app);
...
app.listen(3000);

Use this:

var io = require('socket.io').listen(app.listen(3000));

// or a bit more elaborate:
var server = app.listen(3000);
var io     = require('socket.io').listen(server);

The difference is that the .listen() method of socket.io takes a http.Server instance as argument, which is what Express' .listen() method happens to return.

app itself is an Express instance, which is something else.

Post your server.js snippet, as that is where you problem most likely is. You need to follow the examples on http://socket.io carefully to make sure socket.io is wired up to properly respond with the socket.io client javascript code when the browser requests /socket.io/socket.io.js .

In addition to robertklep answer, please have a look at Socket.IO compatibility section in express wiki pages.

It explains about express 3.x changes which breaks express 2.x and socket.io examples. That's why solution proposed by robertklep is necessary to have socket.io working with the latest version of express server.

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