You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We already have a bit of skeleton code in index.js:
// open up the endpoint /robotJoin to a GET requestapp.get('/robotJoin',(req,res)=>{console.log("req query: ",req.query);letoutput=getDataFromFile();// if this is the first robot who connects, let's set him up as something random.if(output.num_connected==0){output.num_connected++;varROBOT_ID=72;varduck=`{"${ROBOT_ID}": {"robot_state": "rose","robot_eye_color": "photo","robot_expression": "light"}}`;output.robots.push(JSON.parse(duck));saveDataToFile(output);}console.log(output);res.status(200).send('Hello World You are Robot '+output);});
How it works
We see that we set up a GET endpoint at localhost:3000/robotJoin. We open up output.json and then parse through it, and then add a fake robot to the data and save it back up.
As an example, if we empty the contents of output.json and start up the server, we see that the program state has initialized the file to be:
Note that this is after formatting the data to better view it (It's typically in a single line).
Alright, now what?
We can send more information to the server through our GET endpoint.
If we type in localhost:3000/robotJoin?id=0&status=alive into the browser, our req query turns into the following:
Example app listening at http://localhost:3000
req query: { id: '0', status: 'alive' }
GET request parameters
We want to have robots join the server, and provide an initial state on start up.
Therefore, we should fashion a GET request that does the following:
provide an unique identifier for the robot
provide their status:
state of charge
current movement
current eye color
current expression
Our url might look like the following: localhost:3000/robotJoin?id=Blinky&soc=85%&move=ankles ...(and so on)
A list of the current movements are:
Ankles
Demo1
Demo2
Hop
Walk
Wiggle
Eye colors and expressions haven't been determined yet.
After we send this data through the endpoint, they are available from the request query; we can access a parameter from req.query.PARAM. For example, we can access the parameter id with req.query.id.
Dealing with robots joining
Instead of the current code, where we check if nobody is connected and then build a fake robot, we should check for these parameters and build a robot for it in the data if it does not exist yet.
This means getting the file, checking through a list to see if a robot by the same ID exists, and if it doesn't, pushing a new robot with the url parameters.
We can get the list of robots currently in our database with the following:
letoutput=getDataFromFile();letrobots=output.robots;letfound=false;for(leti=0;i<robots.num_connected;i++){letrobot=robots[i];letrobotIDrobots.id;// check to see if the robot ID matches our request ID// if it is, set found to be true}// if we found the robot, do nothing, but if we didn't add it!// we can use lines 29-30 in `index.js` as an example how:// build a robotvarROBOT_ID=72;varduck=`{"${ROBOT_ID}": {"robot_state": "rose","robot_eye_color": "photo","robot_expression": "light"}}`;// then push it to the arrayoutput.robots.push(JSON.parse(duck));// once you're done, save it back to the file!
Finally, once you're able to save the robot to the file and add multiple robots, we should send back a success message. Make it fancy! Update line 35 in index.js for this.
Bonus
If you have this working, now make a /robotLeave endpoint!
The text was updated successfully, but these errors were encountered:
Context
We already have a bit of skeleton code in index.js:
How it works
We see that we set up a
GET
endpoint at localhost:3000/robotJoin. We open upoutput.json
and then parse through it, and then add a fake robot to the data and save it back up.As an example, if we empty the contents of
output.json
and start up the server, we see that the program state has initialized the file to be:We can type in
localhost:3000/robotJoin
into the browser and see that the console has received the request:We can then check our
output.json
and see that it has been updated with fake robot data!Note that this is after formatting the data to better view it (It's typically in a single line).
Alright, now what?
We can send more information to the server through our
GET
endpoint.If we type in
localhost:3000/robotJoin?id=0&status=alive
into the browser, our req query turns into the following:GET request parameters
We want to have robots join the server, and provide an initial state on start up.
Therefore, we should fashion a
GET
request that does the following:Our url might look like the following:
localhost:3000/robotJoin?id=Blinky&soc=85%&move=ankles ...(and so on)
A list of the current movements are:
Eye colors and expressions haven't been determined yet.
After we send this data through the endpoint, they are available from the request query; we can access a parameter from
req.query.PARAM
. For example, we can access the parameter id withreq.query.id
.Dealing with robots joining
Instead of the current code, where we check if nobody is connected and then build a fake robot, we should check for these parameters and build a robot for it in the data if it does not exist yet.
This means getting the file, checking through a list to see if a robot by the same ID exists, and if it doesn't, pushing a new robot with the url parameters.
We can get the list of robots currently in our database with the following:
Finally, once you're able to save the robot to the file and add multiple robots, we should send back a success message. Make it fancy! Update line 35 in
index.js
for this.Bonus
If you have this working, now make a
/robotLeave
endpoint!The text was updated successfully, but these errors were encountered: