Deploying node.js applications


    Application deployment is always a critical point in the development cycle ... and is never easy. If you use the services of hosting providers, then most likely you have already been provided with a sufficient service of all sorts of amenities. In this article, I’ll talk about deploying applications without creating a complex hosting infrastructure ...

    First, let's decide on technology. We will use, of course, only what the development platform provided to us - node.js. A certain web-service will work on the server, which will accept requests and do all the "dirty" work. On the client - command-line tool. Well, how without him?

    So, the service is installed as follows:
    npm install -g node-deploy-server --unsafe-perm
    


    Client, no more complicated ...
    npm install -g node-deploy-client
    


    The software is worth it, now it's time to tell how to configure it in order to make friends with the service with the client.

    Server Tuning.

    The configuration file is named nodehosting.json and is located in the / etc folder for Linux systems and in the root of the module for Windows.
    Full text of the configuration file
    {
        "port" : 15478,
        "username" : "admin",
        "password" : "admin",
        "applications" : {
            "application1" : {
                "path" : "../applications",
                "foreverConfig" : {
                    "cwd" : "../applications/application1"
                },
                "startProcess" : true
            }
        }
    }
    


    • port - TCP port on which the service will work
    • username and password - there's nothing to explain here ...
    • object applications. The property names of this object are the names of the applications for which the service is responsible for deploying.

    Application Setup:
    • path - the shared root folder of applications. Do not worry to create it. They will do everything for you.
    • foreverConfig - Applications are launched using forever-monitor, so I have nothing to tell more than an official source .
    • startProcess - automatically start the process after deployment

    Separately, I want to say about installing dependencies. It is performed using the npm install command in the root folder of the application. For an example it is ../applications/application1. Thus, if you need to perform additional actions during deployment, it is enough to write them in the scripts.install field in package.json

    Client setup

    In the root folder of the application (next to package.json) you need to put a file with the name .deploy and the following contents:
    {
    	"dev" : {
    		"url" : "http://admin:admin@localhost:15478"
    	},
    	"staging" : {
    		"url" : "http://admin:admin@192.168.1.3:15478"
    	}
    }
    

    Unlike the server, it is not a lot here - the very minimum is to contact the server. You can specify several configurations in the file. Those. You can define several different servers for deployment, for example: dev, staging, production. The choice of a specific server is made by the client utility. The actual configuration name in the test case is dev and staging. A more detailed example can be viewed on github

    Server launch.

    We start the server on Linux
    service nodehosting start
    

    Do not forget to run the command chkconfig nodehosting on if you want the service to start when the OS starts.

    We start the server on Windows
    sc start nodehosting.exe
    


    Client launch

    To deploy the application, go to its root folder and run on the command line
    deploy dev
    

    The output from the command looks something like this:


    How it works?

    The client part opens the package.json file and uses the “name” field as the name of the application (not at all strange). Next, it packs the root folder, excluding the node_modules folder from the archive. The resulting archive sends POST via the http protocol to the address specified in the .deploy file. Well, the processes already described above occur on the server.

    Acknowledgments.

    The project is young. So constructive criticism and suggestions are expected. Sources hosted on github

    If suddenly.

    Tested on a couple of RedHat-based distributions, Debian 7.2 (wheezy) and Windows 7.

    PS

    Starting January 2014, a web interface is available. Thus, setting up applications has become much easier.

    And…
    • Before deploying the application, it is not necessary to create a new application through the interface, and generally configure anything. It is enough to fulfill the request from the client and the application will be blocked with default parameters.
    • Switching the server to SSL mode.
    • Tested on the following OS: CentOS 6, Fedora 18, Debian 7.2 (wheezy), Windows 7, Windows 8

    Also popular now: