Saturday, January 19, 2013

Simple way to run javascript code

I was browsing StackOverflow and I came across an unanswered question about a Javascript function.  Before answering the question, I wanted to run the function to make sure I completely understood what was going on.

On Ubuntu

To run a simple Javascript function, in Ubuntu, I followed the advice in this earthviaradio blog post and installed node.js. On Ubuntu, here is what I did.
  • Installed node.js.
  • sudo apt-get install nodejs
  • Wrote a test function. (I saved the file as test.js).
  • var sys=require("util");
    sys.puts("This is a test javascript file.  Hello world!");
  • Ran the function.  ( You should see the output in the console terminal ).
    node test.js

On Mac

If you already have homebrew, you can install node.js with this command.
brew install node
Then, you can run it in the same way as I noted above.

I've heard about nodejs a while ago, but I never spent any time to learn what it was.  This looks like a pretty cool resource to learn about nodejs:
http://www.hongkiat.com/blog/node-js-server-side-javascript/

No comments:

Post a Comment