iOS client consume node js web service

iOS client

Basically keep in mind that when you want to hit a local node js server, the address on the network from the iOS cient’s pov is 192.168.1.102.

You go to System Preferences > Wi-Fi (or Ethernet, or whatever you use to get internet connection ). Then on the right hand side, the status should say ‘connected’ and you’ll see something like “Wi-Fi is connected to TP-LINK_3DF056 and has the IP address 192.168.1.102.”

192.168.1.102 is the network address of the computer that’s running the node js server. Hence when iOS clients look for the server, they will need to hit that address.

We are simply trying to hit a url using GET, and expect json data back.

GET for /

Notice that we use “message” as the key for our json data because in our node code, we are returning a json object using the key “message”.

Node server

POST /setup with parameters ‘name’ and ‘password’

iOS client side

So we use NSDictionary to create dictionary object with keys name and password.
Then, we encode it into a NSUTF8StringEncoding string.

We then use that dictionary string and set it as Http body using setHTTPBody for the request object. We also set our http method to POST using setHTTPMethod on the request object.

Node server side

Checking mongo db via terminal

Open your terminal and type in

mongo

2015-07-05T22:05:14.298+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> show dbs
PersonDatabase 0.078GB
local 0.078GB
> use PersonDatabase;
switched to db PersonDatabase
> db.user.count()
3
> db.user.find()

the result is that Shirley Qian has been inserted:

{ “_id” : ObjectId(“55973f3702e8b0094967b544”), “name” : “rtsao”, “password” : “compaq”, “admin” : true, “__v” : 0 }
{ “_id” : ObjectId(“55977f4b06112d75860f9af6”), “name” : “rtsao6680”, “password” : “abcde12345”, “admin” : true, “__v” : 0 }
{ “_id” : ObjectId(“5599cc4ec1fa2c05496b5c34”), “name” : “Shirley Qian”, “password” : “12345678”, “admin” : true, “__v” : 0 }
>

Full server code