Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17142

Get separate JSON from the response

My team & I are working on a chatting app: an Android client, & a web client.
Recently, we stumbled up a blocking issue. Here is how:

So the web & the Android client (I will refer to them as 'the clients' from now on) communicate with a Node.js server.

We're actually working on the login/signup section. This is an example of what we have so far:

Android client

Briefly, what this code does, is that it sends a parametrized POST request to the /signin route.

Web client

<form ...>...</form>

<script type="text/javascript">
    $('button').click(function(){
        $.post('/signin',{username : $('#username').val(),password :$('#password').val()});
    })
</script>

Server side

// signin post
app.post('/signin', function (req, res) {
    connection.query('SELECT * FROM User WHERE ((username = "' + req.body.username + '" OR email = "' + req.body.username + '") AND password = "' + req.body.password + '")', function (err, rows) {
        if (err) throw err;
        // If the user entered a valid username or email, then check for the password
        if (rows.length > 0 && rows[0].password == rows[0].password) {
            res.render('index', { status: "SUCCESS", username: req.body.username })
        } else {
            // Valid username or email, but invalid password
            res.render('index', { status: "FAILURE", username: req.body.username })
        }
    });
})

The problem

As you can see from the code snippets above, the clients send POST requests to the /signin route, but:

res.render() solves the problem for client 2, res.end({json}) solves it for client 1.

Is there a way we could separate the response, so that each client gets what it wants ?
What is the optimal way to work this out ?

Upvotes: 1

Views: 68

Answers (1)

Rivu Chakraborty
Rivu Chakraborty

Reputation: 1372

You can add an extra variable to get if it is coming from web or android and then do as needed. e.g.-

app.post('/signin', function (req, res) {
    connection.query('SELECT * FROM User WHERE ((username = "' + req.body.username + '" OR email = "' + req.body.username + '") AND password = "' + req.body.password + '")', function (err, rows) {
        if (err) throw err;
        // If the user entered a valid username or email, then check for the password
        if (rows.length > 0 && rows[0].password == rows[0].password) {
            if(req.body.device_type == "web"){
                res.render('index', { status: "SUCCESS", username: req.body.username })
            }
            else{
                res.end({ status: "SUCCESS", username: req.body.username })
            }
        } else {
            // Valid username or email, but invalid password
            if(req.body.device_type == "web"){
                res.render('index', { status: "FAILURE", username: req.body.username })
            }
            else{
                res.end({ status: "FAILURE", username: req.body.username })
            }
        }
    });
})

and in android:-

params.put("postURL", "/signin");
params.put("username", mUsername.getText().toString());
params.put("password", mPassword.getText().toString());
params.put("device_type", "android");
manageSession.execute(params);

in web:-

$.post('/signin',{username : $('#username').val(),password :$('#password').val(),device_type:'web'});

Let me know if there's any more problem.

Upvotes: 1

Related Questions