Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts

Tuesday, March 04, 2014

Starting Node.js App Automatically on Boot

I deployed a node.js app on a server running on CentOS linux. I am using 'forever' to keep my node.js app alive. But when the server restarts, we need to let the app run automatically.

We can modify /etc/rc.local to execute commands on boot. The commands should not run under root. And we should set the environment variable NODE_ENV.
sudo vi /etc/rc.local
Add this line at the end of the file. Change USER_NAME and PATH_TO_PROJECT to your own. NODE_ENV=production means the app runs in production mode. You can add more lines if you need to run more than one node.js app.
su - USER_NAME -c "NODE_ENV=production /usr/local/bin/forever start /PATH_TO_PROJECT/app.js"
Don't set NODE_ENV in a separate line, your app will still run in development mode, because forever does not get NODE_ENV.
# WRONG!
su - USER_NAME -c "export NODE_ENV=production"
Save and quit vi (press ESC : w q return). You can try rebooting your server. After your server reboots, your node.js app should run automatically, even if you don't log into any account remotely via ssh.
You'd better set NODE_ENV environment in your shell. NODE_ENV will be set automatically when your account USER_NAME logs in.
echo export NODE_ENV=production >> ~/.bash_profile
So you can run commands like forever stop/start /PATH_TO_PROJECT/app.js via ssh without setting NODE_ENV again.

Monday, November 05, 2012

Do Not Use CoffeeScript

CoffeeScript is not a framework. It is used to generate readable, clean, tight and safe Javascript code.

Pros:
1. Python style whitespacing
2. Ruby styled lightweight syntax

Javascript:
    var validate = function(form) {
      var required_field_names = ['name','email'];
      return false;
    }
Coffee:
    validate = (form) ->
      required_field_names = ['name','email']
      return false


CoffeeScript returns the result of the last expression in a function
    validate = (form) ->
      required_field_names = ['name','email']
      false

Though there are many reasons to use CoffeeScript.The biggest reason I decided not to use CoffeeScript is debugging. When you debug your web app, you see javascript. What you see is not what you wrote.



Friday, May 11, 2012

Debug Ruby on Rails with Aptana Studio 3

1. Check if ruby-debug-ide is installed

$ gem list

2. If not installed, install ruby-debug-ide

$ gem install ruby-debug-ide

3. Open the project in Aptana Studio 3, App Explorer -> Gear icon's drop down menu -> Debug Server

debug rails with Aptana studio 3

4. Set a breakpoint in a file, e.g. index.html.erb
5. Open http://localhost:3000/<your path>/index.html in your browser
6. Aptana will ask you if open Debug perspective. Of cause. You can see the server stops at the break point.
201205112119.jpg