Category Archives: Node JS

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.

basic node server (pt 3) – request handlers w/non-blocking op

We will inject the response object (from our server’s callback function onRequest()) through the router into the request handlers. The handlers will then be able to use this object’s functions to respond to requests themselves.

First we create an object. An an object in javascript is like a key/value dictionary, where we match keys and values up.

In our case, let’s create a handler object where we basically have the URL as the keys and the names of the functions that corresponds to those URLs as the value.

index.js

The server’s start takes route function parameter and handle object parameter. The handle object was declared in index.js.

The route is a function variable that takes in the handle object, the response object, and pathname that’s in server.js.

It then processes the requests and outputs data using these objects.

Server

From index.js, we remember that handle[“/”] maps to a requestHandler.function1
handle[“/start”] maps to another requestHandler.function2…so on and so forth

Hence, in our case, the pathname represents the user’s path for their request…..and handle[pathname] is the name of the function we will call.

We first want to make a simple check to see if handle[pathname] is indeed in fact a function, if it is…then we call the function like so:

where we throw in the response obj so that we can output to the browser whatever data we get.

router.js

requestHandler.js

Basically the function definitions that responds to the user’s request URLs.
We only provide 2: start and upload, which corresponds to
localhost:8888/start and localhost:8888/upload

1) We use dependency injection of the response object and have it output back to the browser the data we get.

2) We use child processes to make it non-blocking

basic node server (pt 2) – using child process

In pt 1, the code we used is blocking. What this means is that the current task needs to finish before the next one can start.

For example, say the first task of processing localhost:8888/start (which gets routed to the start function ) tasks 10 seconds to finish, you can simulate this by using a sleep(10).

From the Node Beginner book:
“Just to make clear what that does: when the function start() is called, Node.js waits 10 seconds and only then returns “Hello Start”. When calling upload(), it returns immediately, just like before.

(Of course, you should imagine that instead of sleeping for 10 seconds, there would be a real life blocking operation in start(), like some sort of long-running computation like reading a huge table from the db)

How to show this:
“First, open two browser windows or tabs. In the first browser window, please enter http://localhost:8888/start into the address bar, but do not yet open this url!
In the second browser window’s address bar, enter http://localhost:8888/upload, and again, please do not yet hit enter.

Now, do as follows: hit enter on the first window (“/start”), then quickly change to the second window (“/upload”) and hit enter, too.
What you will notice is this: The /start URL takes 10 seconds to load, as we would expect. But the /upload URL doesn’t process because its waiting for the /start URL to finish its 10 second process

Instead, whenever expensive operations must be executed, these must be put in the background, and their events must be handled by the event loop.

What’s going on: The 2 step: exec and finish block

So we spawn a child process to do the long time operation. However all we see is empty in our browser. The concept here is that the child process runs in 2 step. First it executes its task, then when it finishes, it executes its finish block and that’s it.

In our example, after the user hits “localhost:8888/start” in their browser, the main thread goes through the code 1, 2, and when it hits 3, spawns the child process. Then it goes straight down to 4, where the content variable is still “empty”.

When the child process finishes processing the “find” task, it executes its finish block and stops.

Hence that’s why you only see “empty” in your browser.

In other words, it’s clear that exec() does something in the background, while Node.js itself continues with the application, and we may assume that the callback function we passed into exec() will be called only when the “Find” command has finished running.

When you run the code, first, the server sets up:

Rickys-MacBook-Pro:nodeProject0 rickytsao$ node index.js
start of index.js
end of server.js
start of router.js
end of router.js
server variable running function start with parameter router
server.js – start function:
[Function: route]
server.js – Server has started. end of start function
end of index.js

Then you open your browser, run a random url like so:

localhost:8888/hehehehe


server.js – start of onRequest function
server.js – request.url: /heheheh
router.js – about to route a request for /heheheh
server.js – giving result to response obj….

Perfect, so we see that the onRequest function receives the url, gives it to router for process, then returns it to server.js for the response obj to display.

Then, hit the /start url like so:

localhost:8888/start

Okay, so you’ll see that the main thread goes through as usual, hits onRequest function, prints the logs, and as you can see at router.js, it returns “empty” string. It does so because at 4, the content variable still has the string “empty”. The separate child process has asynchronously run its own course to process our Find.


server.js – start of onRequest function
server.js – request.url: /start
router.js – about to route a request for /start
routerl.js – returning content: empty
server.js – giving result to response obj….

When it finishes that Find, it prints the finish block code.


——FIND operation done——–
content is: /Users/RickyTsao/Documents/JDBC
/Users/RickyTsao/Documents/JDBC/.DS_Store
/Users/RickyTsao/Documents/JDBC/Apress – Expert.Oracle.JDBC.Programming.pdf
/Users/RickyTsao/Documents/JDBC/JDBC Recipes- A Problem-Solution Approach %2528Problem-Solution Approach%2529%255B2005%255D.pdf

Therefore, even though we solved the blocking problem by using a child process to run it in a non-blocking way, we still need to make sure it returns the results correctly. We do so by injecting the response object into the start function.

Better yet, we should have all request handler functions be grouped into a requestHandler.js, which has the response obj injected. And whenever a child process finishes it task and runs to its finish block code, it can use response and correctly output the results.

We do all of this in part 3.

full code for part 2

index.js

server.js

router.js

basic node server (pt 1)

We first start at index.js. Basically it first requires the module files ‘server’ and ‘router’ and recognizes the interface and definition implemented in those files. It puts the function definitions and variables into global var server an router for us to use later.

Hence that’s why you will see the console.logs of those files


start of index.js
start of server.js
end of server.js
start of router.js
end of router.js

…then after requiring those files, we get display the log:
‘server variable running function start with parameter router’

Our variables to use are server and router.

in index.js

We now use the server variable to call function start.

The whole point of router.js is to provide the function route, which we pass into the server.start for it to use in its onRequest function.

in index.js

As you will see later, each request that comes from a browser gets taken care of by onRequest. And onRequest uses the route function in order to show user the url path.

in server.js

In other words, we pass it in simply so that it is a variable that can be used in the start function. The start function’s function onRequest(request, response) directly uses the route function.

function onRequest(request, response) is just a function definition of start function and does not get used yet.

We have our global variable http and call its function createServer, specifying that we want to create a server and it listens for user requests at port 8888.

We then pass in the onRequest function as a parameter to createServer to let it know what function to use to process when a request comes in.

Start up your servers by opening up a terminal, going into the directory where index.js, server.js, and router.js are located, and in your terminal type:


node index.js

you should see that the server will start.

Rickys-MacBook-Pro:nodeProject1 $ node index.js
start of index.js
start of server.js
end of server.js
start of router.js
end of router.js
server variable running function start with parameter router
server.js – start function:
[Function: route]
server.js – Server has started. end of start function
end of index.js

Then open up a browser and type in:

localhost:8888/test

You will then see result in your terminal:

server.js – start of onRequest function
server.js – request.url: /test
router.js – about to route a request for /test

full code

index.js

server.js

router.js

Installing NodeJS on Mac

ref – http://stackoverflow.com/questions/11177954/how-do-i-completely-uninstall-node-js-and-reinstall-from-beginning-mac-os-x

Install Homebrew

Homebrew is a package manager for the Mac — it makes installing most open source sofware (like Node) as simple as writing brew install node. You can learn more about Homebrew at the Homebrew website. To install Homebrew just open Terminal and type:

You’ll see messages in the Terminal explaining what you need to do to complete the installation process.

result:

Last login: Sat Jun 6 05:15:33 on console
Rickys-MacBook-Pro:~$ ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/…
/usr/local/share/man/man1/brew.1
==> The following directories will be made group writable:
/usr/local/lib/pkgconfig
/usr/local/share/man/man3
/usr/local/share/man/man5
/usr/local/share/man/man7

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/lib/pkgconfig /usr/local/share/man/man3 /usr/local/share/man/man5 /usr/local/share/man/man7

WARNING: Improper use of the sudo command could lead to data loss
or the deletion of important system files. Please double-check your
typing when using sudo. Type “man sudo” for more information.

To proceed, enter your password, or type Ctrl-C to abort.

Password:
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew…
remote: Counting objects: 3590, done.
remote: Compressing objects: 100% (3433/3433), done.
remote: Total 3590 (delta 28), reused 667 (delta 19), pack-reused 0
Receiving objects: 100% (3590/3590), 2.86 MiB | 322.00 KiB/s, done.
Resolving deltas: 100% (28/28), done.
From https://github.com/Homebrew/homebrew
* [new branch] master -> origin/master
Checking out files: 100% (3594/3594), done.
HEAD is now at 231c429 unac: fix audit warnings
==> Installation successful!
==> Next steps
Run brew help to get started
Rickys-MacBook-Pro:~ $

Installation

Removing previous installations of NodeJS

If its your first time installing nodeJS, then skip this part go on to the standard installation instructions below

However, if you have previous versions of nodeJS installed, you need to remove previous node folders and files.

  • go to /usr/local/lib and delete any node and node_modules
  • go to /usr/local/include and delete any node and node_modules directory
  • if you installed with brew install node, then run brew uninstall node in your terminal
  • check your Home directory for any local or lib or include folders, and delete any node or node_modules from there
  • go to /usr/local/bin and delete any node executable
  • remove /usr/local/lib/dtrace/node.d
  • remove /usr/local/share/man/man1/node.1

Usually every time you do an install, and if there are any issues, it will print out for you…something like this:

Error: The brew link step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink share/man/man1/node.1
Target /usr/local/share/man/man1/node.1
already exists. You may want to remove it:
rm ‘/usr/local/share/man/man1/node.1’

Just solve the error, do a “brew uninstall node”, and start the installation all over again.

Actual installation

Installing Node.js and NPM is pretty straightforward using Homebrew. Homebrew handles downloading, unpacking and installing Node and NPM on your system. The whole process (after you have XCode and Homebrew installed) should only take you a few minutes.

Open the Terminal app and type brew install node

Sit back and wait. Homebrew downloads some files and installs them. And that’s it.

After you entered the command you should see the installation go under way:

Rickys-MacBook-Pro:~ rickytsao$ brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-0.12.4.yosemite.bottle.tar.
######################################### 75%

==> Downloading https://homebrew.bintray.com/bottles/node-0.12.4.yosemite.bottle.tar.
Already downloaded: /Library/Caches/Homebrew/node-0.12.4.yosemite.bottle.tar.gz
==> Pouring node-0.12.4.yosemite.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
/usr/local/etc/bash_completion.d
==> Summary
🍺 /usr/local/Cellar/node/0.12.4: 2590 files, 28M
Rickys-MacBook-Pro:~ rickytsao$

After installation, check what version of Node and NPM you’ve just installed:

To see if Node is installed, type node -v in Terminal. This should print the version number so you’ll see something like this v0.10.31.
To see if NPM is installed, type npm -v in Terminal. This should print the version number so you’ll see something like this 1.4.27

How to Update Node and NPM

New versions of Node and NPM come out frequently. You can use Homebrew to update the software it installs.
Make sure Homebrew has the latest version of the Node package. In Terminal type brew update
Upgrade Node: brew upgrade node

How to Uninstall Node and NPM

You can use Homebrew to uninstall packages that it installed: brew uninstall node