Reputation: 17142
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:
MainActivity.java
Button loginButton = (Button) findViewById(R.id.loginButton);
loginButton.setOnClickListener(new LoginListener());
private class LoginListener implements View.OnClickListener {
@Override
public void onClick(View v) {
CustomAsyncTask manageSession = new CustomAsyncTask();
Map<String, String> params = new HashMap<>();
params.put("postURL", "/signin");
params.put("username", mUsername.getText().toString());
params.put("password", mPassword.getText().toString());
manageSession.execute(params);
}
}
Briefly, what this code does, is that it sends a parametrized POST request to the /signin route.
<form ...>...</form>
<script type="text/javascript">
$('button').click(function(){
$.post('/signin',{username : $('#username').val(),password :$('#password').val()});
})
</script>
// 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 })
}
});
})
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
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