- in the HTML folder, there is a index.html and main.js
- in the main.js, there is an ajax call to read information and create dom elements for that information.
- The easiest way to test a connection is to use dummy data.
- There is a dummy data file called html/test.js .
- Connect the ajax call to test.js to see what the functionality of the front end should look like before creating the back end.
- The animal.js is what your backend should generate when we get it operational.
- Once operational, run the index.html file through MAMP
- start the MAMP server. Make sure the Apache checkbox is green.
- click on "open web start page"
- remove
MAMP/?language=English
or anything like that from the URL and press enter - Navigate to the appropriate folder of your animalExplorer prototype
- By default, when you get into the html folder, the index.html file should run
- If animals appear, it is working correct.
- Serve the data up via Node
- Open the webserver.js file
- In the same folder as webserver.js,
- open you terminal
- npm install express
npm install express
- in the webserver.js file:
- require express and load its contents into a variable named express
- Use the express variable and run it like a function. Put the contents into a variable called webserver
- make your webserver listen to port 3000
webserver.listen( PORT, FUNCTION TO CALL WHEN LISTEN HAS HAPPENED)
- in the webserver.js file:
- run your webserver in node and make sure it doesn't explode. Quit the server
control + c
to do the next test. - Add a middleware static file server
- webserver.use() is the function that loads middle ware
- you want it to use the following as a parameter:
- express.static() sets a static folder to serve files from
- give it the following combined parameter:
- you need to give it the current directory
__dirname
(note the double underscores) - concatenate onto the end of __dirname the directory
/html
so that it picks the html folder to serve files from
- you need to give it the current directory
- at the end you should have
webserver.use ( EXPRESS STATIC STUFF ( THE DIRECTORY TO SERVE ) )
- Run your server and test the file is loading by going to your browser and navigating to
localhost:3000
and see if it pulls up your index.html file. It whoudl automatically pick up the test.html in the same folder as well as the images.
- run your webserver in node and make sure it doesn't explode. Quit the server
- in the webserver.js file:
- at the top of the file, import the animal.js file from the data folder. Import it into a variable called animals
- between the webserver.use and the webserver.listen add an endpoint listener
- add webserver.get(). It will take an argument for the path to map to and the function to run when a connection happens
- the path should be '/data'
- the function should take parameters of
request
andresponse
- inside the function, json stringify the animal variable from above into a new string jsonAnimals
- use
response.send
to send the jsonAnimals information to the requesting client
- add webserver.get(). It will take an argument for the path to map to and the function to run when a connection happens
- run the server and test the code by going directly to
localhost:3000/data
to see if you get appropriate JSON
- change your ajax URL to
localhost:3000/data
and see if your data loads appropriately.
-
In the data.php file (in the html folder)
-
load the data from test.js via an file_get_contents('test.js') into a variable called $animalsJSON
-
convert the $animalsJSON into a PHP variable
- use json_decode
- store it into $animals.
- be sure to give it a second variable of true to decode it into associative arrays rather than php standard objects
- we don't need to do this normally, we're having fun with php! loading in data via json!
-
let's sort the data, use this code
function sortFunction($a, $b) {
return $a["name"] <=> $b["name"];
}
usort($animals, "sortFunction");
- very similar to the JS sort function, don't you think?
- encode the data back into json
- use json_encode
- save into the variable $sortedAnimals
- print the $sortedAnimals variable
- run the data.php file to make sure it shows the data correctly
- go to your index.html file
- change your ajax destination to
data.php
- make sure it works!
- change your ajax destination to