Developing a Node.js Application
Below is a simple demonstration of deploying a small web application to a SmartMacine web server.
In this topic:
Use the following instructions to write a simple web server called "Hello, world!" in Node.js.
- Create a directory on your SmartMachine:
- Create a server.js file and write the following:
- Save the file to ~/hello-world/server.js.
You can get copy of this from here.
- Start the server:
- Test the server by pointing a browser to the public IP of your SmartMachine.
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.
- Download the example "Hello, world!" manifest file and add it to the system configuration with the following:
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.
- Start the Service:
You can use disable to stop the service.
- 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!
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:
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:
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.|
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:
Once you fix whatever it is that is preventing the service from starting properly, you can restart it by doing the following:
|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:
You can then test your site by pointing a web browser at your domain or curl http://127.0.0.1/.