Developing a Node.js Application

Modified: 05 Jan 2015 18:06 UTC

Below is a simple demonstration of deploying a small web application to a SmartMacine web server.

In this topic:

At a Glance

This topic shows you how to write a simple web server in Node.js.

Writing a Node.js Application

Use the following instructions to write a simple web server called "Hello, world!" in Node.js.

  1. Create a directory on your SmartMachine:
    mkdir hello-world
    
  2. Create a server.js file and write the following:
    require("http").createServer(function (req, res) {
    res.writeHead(200, {})
    res.end("Hello, world\!")
    }).listen(80)
    console.log("waiting to say hello.")
    
  3. Save the file to ~/hello-world/server.js.
    You can get copy of this from here.
  4. Start the server:
    $ which node
    /opt/local/bin/node
    
    $ node hello-world/server.js
    Password:
    waiting to say hello.
    
  5. Test the server by pointing a browser to the public IP of your SmartMachine.

Define the Service

You can use the Service Management Facility (SMF) to manage services you define. A service in Solaris is a first-class citizen and is associated with a manifest file that defines it. The following instructions use SMF commands to define and start a "Hello, world!" service.

  1. Download the example "Hello, world!" manifest file and add it to the system configuration with the following:
    wget [http://github.com/isaacs/joyent-node-on-smart-example/raw/master/node-hello-world-service-manifest.xml]
    svccfg import node-hello-world-service-manifest.xml
    
    A copy of the example "Hello, world!" manifest file is here.
    If you clone the git repo, you can use SMF to grab the manifest from there instead.
  2. Start the Service:
    svcadm enable node-hello-world-service
    
    You can use disable to stop the service.
  3. Point a web browser to the public IP of your SmartMachine.

If the browser renders a "Hello, world!" page, congratulations! Your Node.js web server is running successfully!

Troubleshooting

A service in Solaris is associated with a log file of the same name. In this case, the log file is stored at /var/svc/log/site-node-hello-world-service:default.log. If a problem with a service occurs, you can troubleshoot it by looking at the service log. For example, you can view the last few log messages like this:

tail -n 50 "/var/svc/log/site-node-hello-world-service:default.log"

SMF will attempt to restart a service if it stops unexpectedly. However, if the service keeps crashing, SMF will place the service in "maintenance" mode. For example:

...
[ Sep 14 00:55:56 Stopping because all processes in service exited. ]
[ Sep 14 00:55:56 Executing start method ("/home/admin/local/bin/node /home/admin/hello-world/server.js"). ]
[ Sep 14 00:55:56 Stopping because all processes in service exited. ]
[ Sep 14 00:55:56 Executing start method ("/home/admin/local/bin/node /home/admin/hello-world/server.js"). ]
[ Sep 14 00:55:56 Stopping because all processes in service exited. ]
[ Sep 14 00:55:56 Executing start method ("/home/admin/local/bin/node /home/admin/hello-world/server.js"). ]
[ Sep 14 00:55:56 Stopping because all processes in service exited. ]
[ Sep 14 00:55:56 Restarting too quickly, changing state to maintenance. ]

If this happens, ensure the site is working if you start the service manually. This uncovers most problems.

For this command, sudo is required because "Hello, world!" is listening on port 80.
sudo /home/admin/local/bin/node /home/admin/hello-world/server.js

You can also get information about a service by using the svcs command. For example, if "Hello, world!" thrashes itself into maintenance mode, you might see this:

$ svcs -lp node-hello-world-service
fmri         svc:/site/node-hello-world-service:default
name         node.js hello-world service
enabled   true
state       maintenance
next_state   none
state_time   September 13, 2010  5:55:56 PM PDT
logfile   /var/svc/log/site-node-hello-world-service:default.log
restarter   svc:/system/svc/restarter:default
contract_id
dependency   require_all/refresh svc:/milestone/network:default (online)
dependency   require_all/refresh svc:/system/filesystem/local (online)

Once you fix whatever it is that is preventing the service from starting properly, you can restart it by doing the following:

svcadm disable node-hello-world-service
svcadm enable node-hello-world-service
Once a service goes into maintenance mode, you must disable it before you can re-enable it.

At this point, the log should look like this:

$ tail -5 /var/svc/log/site-node-hello-world-service\:default.log
[ Sep 14 00:55:56 Restarting too quickly, changing state to maintenance. ]
[ Sep 14 01:09:04 Leaving maintenance because disable requested. ]
[ Sep 14 01:09:04 Disabled. ]
[ Sep 14 01:09:07 Enabled. ]
[ Sep 14 01:09:07 Executing start method ("/home/admin/local/bin/node /home/admin/hello-world/server.js"). ]

You can then test your site by pointing a web browser at your domain or curl http://127.0.0.1/.