Reputation: 15080
I want to create a login script using Nodejs. At the moment I am considering using sockets or ajax posts.
I have installed Nodejs, and used this code.
I began with a simple Ajax post. I used jQuery for this and I serialized the form. This worked but sometimes it has some delay or it was slow.
So I thought maybe sockets is a better way to communicate with the server and back to the client.
Now I'm facing the problem that I can't use serialize()
anymore (damn I loved that serialize()
function).
So now I have to use json in my client, getting the 2 input values (email and password), make an object called obj
, add the values and use socket.emit('login', JSON.stringify(obj, null, 2))
In my server I have
socket.on('login', function(data){
data = JSON.parse(data)
if(data.email === '[email protected]' && data.password === 'secret')
// return true
// return false
})
Now the problem is that I am unable to let my client know if the credentials are correct.
I could do this by using another emit
but I think this would be overload.
Can't I just use return true
or maybe socket.send(true)
?
Anyone have the same problem? Or maybe the solution to my problem?
Thanks!!
Upvotes: 3
Views: 138
Reputation: 5319
Well, dude, it smells like it wont be the only function that you will call from your client. So you should start designing your API.
If you send JSON from client, then the most honorable thing to do from the server is to return JSON. I would use socket.send.
A basic OK response would look like this:
{ "Status" : true }
A basic Error response would look like this:
{ "Status" : false, "Message": "Login or password not valid" }
If in the future you need to return data to your client, then the same basic entity might be used you just need to add more attributes to it and read it in the client.
{ "Status" : true, Data: { /* data here */ } }
Upvotes: 3