Saturday, March 14, 2015

A Node.js Rookie Diary

To run your code in a browser, you write it in javascript these days. With node.js, the server-side code can be written in jabascript too. Both sides need to be configurable, need to support dependency injection. Finally, the server side needs to be an HTTP server. The package.json file below reflects my picks on these dependencies:

{
  "name": "webapp.js",
  "version": "1.0.0",
  "description": "A web application with common dependencies",
  "main": "./lib",
  "scripts": {
    "test": "grunt nodeunit"
  },
  "dependencies": {
    "konfy": "0.1.3",
    "dijon": "0.6.1",
    "flatiron": "0.4.3",
    "director": "1.2.8",
    "http-server": "0.7.5"
  },
  "author": "Alec Missine",
  "license": "MIT"
}

 I created it with npm init, then manually edited the dependencies part. Having installed these dependencies in your $WEBAPP_HOME with npm install, you now can

cd $WEB_HOME
node $WEBAPP_HOME/node_modules/http-server/bin/http-server

The static content of your $WEB_HOME will be served on port 8080. If you do not want to cd $WEB_HOME, you can node $HTTP_SERVER_HOME/bin/http-server $WEB_HOME.

To familiarize myself with http-server, I ran

node-inspector &
node --debug node_modules/http-server/node_modules/union/examples/simple/simple.js

in one terminal window, and

curl -d "data to post" http://localhost:9090/foo

in another, while directing my Chrome browser to http://127.0.0.1:8080/debug?port=5858 .  In the browser, I was able to debug the simple.js code.

No comments:

Post a Comment