How to package & deploy Node.js + express web application?

KBJ picture KBJ · Jan 21, 2014 · Viewed 66.2k times · Source

I am new to Node.js programming and I have recently created a sample working web application using (express, backbone & other complimentary view technologies, with mongoDB). Now i am at a point where I want to deploy the same on a staging environment and I am not sure how to package this application and distribute the same. [I can take care of mongoDb and setting it up seperately]

I am from Java world and in there we create jars for reusable libs and war/ear packages for web applications which is deployed in a servlet container. Now in this case since node.js itself acts as a web container as well, how do i package my webapp?

  1. Is there any standard format/guidelines of packaging node webapps built using express? (Is there a similar jar/war packaging systems for node apps?)
  2. How do I deploy it once packaged? Would it become an exe, since it is also its own container?

PS: As of now I am thinking of just manually copying all the required source files into the staging environment and run npm commands to download all dependencies on that machine and then use 'forever' or some other mechanism to run my server.js. (Also, add some sort of monitoring, just in case app crashes and forever fails) I am not sure if that is the right way? I am sure there must be some standardized way of addressing this problem.

Answer

Hüseyin BABAL picture Hüseyin BABAL · Jan 21, 2014

Deploying Node.js applications is very easy stuff. In maven, there is pom.xml. Related concept in Node.js is package.json. You can state your dependencies on package.json. You can also do environmental setup on package.json. For example, in dev environment you can say that

I want to run unit tests.

but in production;

I want to skip unit tests.

You have local repositories for maven under .m2 folder. In Node.js, there is node_modules folder under your Node.js project. You can see module folders with its name.

Let's come to the grunt part of this answer. Grunt is a task manager for your frontend assets, html, javascript, css. For example, before deployment you can minify html, css, javascript even images. You can also put grunt task run functions in package.json.

If you want to look at a sample application, you can find an example blog application here. Check folder structure and package.json for reference.

For deployment, I suggest you heroku deployment for startup applciations. You can find howto here. This is simple git based deployment.

On project running part, simply set your environment NODE_ENV=development and node app.js. Here app.js is in your project.

Here is relative concept for java and nodejs;

  1. maven clean install => npm install
  2. .m2 folder => node_modules(Under project folder)
  3. mvn test => npm test(test section on package.json)
  4. junit, powermock, ... => mocha, node-unit, ...
  5. Spring MVC => Express.JS
  6. pom.xml => package.json
  7. import package => require('module_name')