basic JSON data movement via node and express

install express

You will then see node_modules folder in your project directory. Inside node_modules, you’ll see express framework folder. You are now ready to use express.

In your project directory, create an app.js file.

Then use require function to load in the express library:

Then get the application instance by calling express().
After that let’s designate the folder ‘public’ to be static and serve up web pages from there. We place all web pages into the ‘public’ folder from now on.

Finally, we use function ‘get’ to receive GET events and then have a handler to process that GET event. In our case, we simply create a local array on the stack…and simply throw it back with the response object.

app.js code

public/index.html code

Create a public folder in your project directory. Throw in the image blocks.png. Then create index.html and Put in the code below:

Basically, we check to see if jQuery library is loaded. We then load in the block.png image and also load in client.js file.

public/client.js

This javascript is embedded in the index.html file. Once the page loads, we use jQuery’s

method to load data from the server using HTTP GET request. It requests the url ‘/blocks’ from the server, and uses the appendToList function to process whatever data comes back with the response object.

When it comes back…we create an empty array, and fill it with the data from the response object. For every index i in blocks, we get the text and wrap it inside of li tags. Then push it onto our empty array.

Finally, we have our ul .block-list add the list of li tags.

client.js code

Run it

Go into the project directory where app.js and folders public, node_modules are, and in your terminal go:

node app.js

Then open a browser and

localhost:3000/index.html

You will see that it runs the code in our html from top to down…including our client.js

The client.js will trigger the GET event call towards the browser which will return an array of words to our page. Then It puts the text into li tags, and in turn appends the list of li tags onto a ul tag to be displayed on the browser.