不使用socket.io的简单Node.js聊天程序 [英] Simple Node.js chat program NOT using socket.io

查看:107
本文介绍了不使用socket.io的简单Node.js聊天程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习Node并构建一个简单的聊天应用程序.似乎每个人都使用socket.io.我想了解如何使用get和post在更基础的级别上执行此操作.

I am trying to learn Node and build a simple chat application. It seems like everyone uses socket.io. I would like to understand how to do this on a more fundamental level using get and post.

基本上,我要做的就是拥有一个接受输入并将其重新发布到该表单下方的表单,以供所有人查看.

Basically, all I want to do is have a form that takes an input and reposts it below the form for everyone to see.

这是我到目前为止所拥有的:

This is what I have so far:

//Requirements
var express = require('express'); 
var app = express(); 

//GET
app.get('/', function (req, res) {
 // res.send('Hello World!');
        var response =
          "<HEAD>"+
                  "<title>Chat</title>\n"+
          "</HEAD>\n"+
          "<BODY>\n"+
                    "<FORM action=\"/\" method=\"get\">\n" +
                            "<P>\n" +
                                   "Enter a phrase: <INPUT type=\"text\" name=\"phrase\"><BR>\n" +
                                   "<INPUT type=\"submit\" value=\"Send\">\n" +
                           "</P>\n" +
                   "</FORM>\n" +
          "<P>phrase</P>\n"+
          "</BODY>";
        var phrase = req.query.phrase;
        if(!phrase){
                res.send(response);
        }else{
                res.send(response);
                res.send(phrase);
        }
});

//For testing
app.get('/test', function(req, res){
        res.send('I am a robot');
        console.log('told visiter I am a robot');
});

//Run the app
var server = app.listen(8080, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('App listening at http://%s:%s', host, port);
});

我一直在尝试很多事情,但是我很沮丧.

I've been trying a bunch of things, but I am pretty stumped.

推荐答案

您是否听说过消息传递后端 jxm.io ?

Did you hear about messaging backend jxm.io?

它与 JXcore (Node.JS的开源fork)一起使用. JXM本身是一个开源项目,您可以在github上找到它: jxm .

It works with JXcore (open sourced fork of Node.JS). JXM itself is an open source project, which you can find on github: jxm.

这确实非常快捷高效,您可以查看一些教程.例如,以下是最少的代码,您需要在服务器端运行:

It's really fast and efficient, you can check some tutorials. For example, below is minimal code, that you need to run on server-side:

var server = require('jxm');
server.setApplication("Hello World", "/helloworld", "STANDARD-KEY-CHANGE-THIS");
server.addJSMethod("serverMethod", function (env, params) {
   server.sendCallBack(env, params + " World!");
});
server.start(); 

可以在此处找到客户端部分: 浏览器客户端(JavaScript)

The client's part can be found here: Browser Client (JavaScript)

JXM还支持Java客户端(在android上运行)和节点客户端.

JXM also supports Java clients (runs on android) and node clients.

这篇关于不使用socket.io的简单Node.js聊天程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆