简体   繁体   English

Node.js和Jade

[英]Node.js and Jade

I'm pretty good with javascript but I am fairly new to node.js and jade. 我对javascript非常好,但我对node.js和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. 我正在尝试使用socket.io创建一个基本的聊天服务器(遵循我在网上找到的教程),但是我无法启动并运行它。

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. 首先,我创建了一个基本的package.json文件,其中包含以下内容,在运行时在我的工作目录中创建了node_modules目录。

{
    "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. 然后我创建了server.js文件和script.js文件。 Next I created the home.jade file for the UI. 接下来,我为UI创建了home.jade文件。

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. 当我启动我的服务器并转到127.0.0.1:3000时,页面显示但在错误控制台中它显示GET http://127.0.0.1:3000/socket.io/socket.io.js 404 (Not Found)错误。

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. 我以为我正确引用了文件,但我猜不是:(我的思维过程是因为server.js文件渲染了home.jade文件,我只需要做script(src='socket.io/socket.io.js')在jade文件中。

Any hint in the right direction would be greatly appreciated! 任何暗示正确的方向将不胜感激!

EDIT 1 编辑1
Here is my server.js snippet 这是我的server.js片段

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. 您的socket.io初始化不正确。

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. 不同之处在于socket.io.listen()方法将http.Server实例作为参数,这就是.listen()方法碰巧返回的内容。

app itself is an Express instance, which is something else. app本身就是一个Express实例,这是另一回事。

Post your server.js snippet, as that is where you problem most likely is. 发布您的server.js代码段,因为这是您最有可能遇到的问题。 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 . 您需要仔细遵循http://socket.io上的示例,以确保当浏览器请求/socket.io/socket.io.js时,socket.io已连线以正确响应socket.io客户端javascript代码。

In addition to robertklep answer, please have a look at Socket.IO compatibility section in express wiki pages. 除了robertklep的回答,请查看快捷维基页面中的Socket.IO兼容性部分。

It explains about express 3.x changes which breaks express 2.x and socket.io examples. 它解释了有关express 3.x打破变化express 2.xsocket.io例子。 That's why solution proposed by robertklep is necessary to have socket.io working with the latest version of express server. 这就是为什么robertklep提出的解决方案必须让socket.io与最新版本的express服务器一起工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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