在POST中使用.emit [英] Using .emit from POST

查看:114
本文介绍了在POST中使用.emit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用 socket.io 发送到 node.js 的post方法中的当前套接字 em>和表达,而无需经过 io.sockets.on('connection')

Is there a way to emit to the current socket within a post method of node.js using socket.io and express without having to go through the io.sockets.on('connection')?

这是我的问题。我正在做一个迷你授权系统。当用户从表单提交时,通过'/ login'发布数据,而不是使用 onclick emit。如果用户具有无效的用户名或密码,则应发送消息通知失败。我不喜欢使用回调或写入,因为我使用的是一个 Jade 模板。您可以看到我的源代码 here

Here is my issue. I am making a mini authorization system. When the user submits from the form it post the data through '/login' instead of using a onclick emit. If the user has an invalid user-name or password it should send a message back notifying it failed. I would prefer not to use the callback or write because I am using a Jade template. You can see my source code here.

示例:
服务器端

Example: Server Side

var LoggIn = require('./lib/login.js'); // <-- Middle-ware
app.post('/login', function (req, res){
    LoggIn.authenticate(req.body.user, req.body.password, function(user, msg) {
        if (user) {
            req.session.user = user;
                res.redirect('/');
        } else {
            res.render('login');
            //res.emit('loginFail', 'You have entered an invalid user / password!<br>Try Again.); // <-- wishing for something like this
                console.log('Login Failed! : ' + msg);
        }
    });
});

示例
客户端

JS

var status = document.getElementById('status');
socket.on('loginFail', function(msg) {
    status.innerHTML = msg;
});

Jade Form

Jade Form

#status
form#loginForm(method='POST', action='/login')
            input(name='user', placeholder='User Name')
            br
            input(name='password', placeholder='Password', type='password')
            br
            input(value='Sign In', type='submit')


推荐答案

我不认为有办法再次使用io.sockets.on('connection')来执行此操作。如果您正在使用res.render,您可以通过以下方式传递数据:

I don't think that there is a way to do this without using io.sockets.on('connection') again. If you're using res.render anyway, you could pass data through that though:

res.render('login', {msg: 'You have entered an invalid user / password!<br>Try Again.'});

然后在您的玉文件中执行此操作,以打印出消息(如果存在):

and then do something like this in your jade doc to print out the message if it exists:

- if (msg)
     p #{msg}

但是,这样做的问题是它重新加载页面,如果您只是想显示一个简单的反馈消息,我不认为你想要的。您可以使用jQuery在客户端提交表单:

But the problem with doing that is that it reloads the page, which I don't think you want if you're just trying to display a simple feedback message. You could use jQuery to submit the form on the client side instead:

    $('#myForm').submit(function(e){
        e.preventDefault();
        $.post(
            $(this).attr("action"), // The URL to send form data to
            $(this).serialize(), // Serializes the form data to be sent
            // Success callback function
            function(data){ 
                if(data.url){
                    window.location.href = data.url;
                } else{
                    $('#msg').html(data.msg);
                }
            }
        );
    });

在服务器端,您将具有:

On the server-side you would have:

app.post('/login', function(req, res){
    // do whatever you need to check if the login is valid
    if(validLogin){
        res.send({url: myURL}); // send url that user should be redirected to
    } else{
        res.send({msg: 'You have entered an invalid user / password!<br>Try Again.'});
    }
});

所以如果登录有效,重定向的URL将发送回浏览器,用户是重定向。如果登录无效,则会发回并显示错误信息。查看此问题了解更多信息关于提交表单,然后通过jQuery重定向。

So if the login is valid, the URL to redirect is sent back to the browser and the user is redirected. If the login is not valid, the error message is sent back and is displayed. Check out this question for more about submitting a form and then redirecting via jQuery.

这篇关于在POST中使用.emit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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