0
    var admins = db.collection('admin');
    app.post('/form-data', urlencodedParser, function (req, res) {
           response = {
               Username: req.body.Username,
               Password: req.body.Password
           };
           console.log(response);
           var exists = false;
           admins.find().toArray(function (err, key) {
               var length = key.length;
               if (key[0].username.toLowerCase() === req.body.Username.toLowerCase() && key[0].password.toLowerCase() === req.body.Password.toLowerCase())
               {
                   res.redirect('/chat/');
                   app.get('/chat/', function (req, res) {
                       res.render('chat');
                   });
               }
               else
               {

                   res.redirect('/');
                   app.get('/', function (req, res) {
                       res.render('index');
                       console.log("palanivelm");
                       socket.emit('messages', 'username & password is incorrect');
                   });
                   //io.on('connection', function(client) {
                   //   console.log('Client connected...');
                   //   client.emit('messages', 'username & password is incorrect');
                   //});
               }
           });

I am trying to store mongolab(db)one username and password statically to collect the mongolab into my actual coding to comapare from username and passowrd. Everything will be access correctly but I cannot add from the socket.io connections in post method.

How to add socket.io (on or emit) in post method?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Palanivel
  • 9
  • 3

1 Answers1

0

Move the io.connection out of app.post, and record the client in another variable

var ioClient = null;
io.on('connection', function(client) {
      console.log('Client connected...');
      ioClient = client;
});

Then to emit message in the app.post as below,

app.post('/form-data', urlencodedParser, function (req, res) {
     ...
     // handling socket.io
     if (ioClient)
        ioClient.emit('messages', 'username & password is incorrect');             
});
zangw
  • 43,869
  • 19
  • 177
  • 214
  • I am already try to this method to username and password not equal in the sence client.emit using display the error alert in my admin login page.But i am refresh the page the error alert cannot remove in admin page.How to remove the error messages? – Palanivel Jan 23 '16 at 08:34
  • when you refresh the page, the `post` is resend to the server? – zangw Jan 23 '16 at 08:40
  • When you refresh the page, new web socket will be created. So I guess some additional `post` request was sent from client. – zangw Jan 23 '16 at 08:42
  • When you refresh the page, new `post` request will be sent from client to server? if so, I think it is not good design. It will be better to send `post` request from client through one `button` form or something else.. – zangw Jan 23 '16 at 08:45
  • ok sir...let me know i check it. How to some additional post request send it? – Palanivel Jan 23 '16 at 08:46
  • ok .....i am trying to another method to post from the data.I ask another Questions? – Palanivel Jan 23 '16 at 08:49
  • my node.js server side add from io.on('connection', function(client) – Palanivel Jan 23 '16 at 08:50
  • @Palanivel, please check this one http://stackoverflow.com/questions/19484247/replace-html-form-on-submit-with-jquery, – zangw Jan 23 '16 at 08:51
  • @Palanivel, If you have issues with `post`, another question should be asked here. I think this question has been solved till now... – zangw Jan 23 '16 at 08:54
  • @Palanivel, I am not clear what you want. Maybe it could be another question with more codes sample... – zangw Jan 23 '16 at 09:02
  • ok.what the difference between io.socket.on(connection) vs io.on(connection)? – Palanivel Jan 23 '16 at 09:08
  • refer to this question. http://stackoverflow.com/questions/24266910/io-onconnection-vs-io-sockets-onconnection – zangw Jan 23 '16 at 09:10
  • @Palanivel, Once you have question please use google or just search it in SO... I am not googler... – zangw Jan 23 '16 at 09:11