How to deploy MEAN.js (Node.js) application to Pro

2019-03-07 20:24发布

问题:

MEAN.JS stack proposes the "grunt build" task for preparing application to Production. Unfortunately there is a lack of information about next steps. Actually it's not clear how to deploy the application to production and how to launch it.

Question #1 What must be configured in the project in addition to changes in the config/env/production.js? E.g. how to work with custom fonts?

Question #2 Ok. The code deployed to Production (via Git, rsync, etc). Is it enough to run it as

 $NODE_ENV=production node server.js& 

回答1:

I recommend to do the following steps for deployment to production environment:

  1. Double check the /config/env/production.js config file and include all assets which you added manually into /config/env/all.js. It's a good approach to use minified versions for production.

  2. (Optional) If you have custom fonts in your app I do recommend to update gruntfile.js and add a task which will copy fonts into /public/dist/ folder. I did the following changes:

    copy: {
        main:{
            expand: true,
            flatten: true,
            src: ['public/modules/core/fonts/*'],
            dest: 'public/dist/',
            filter: 'isFile'
        }
    },
    
    ...
    // Build task(s).
    grunt.registerTask('build', ['lint', 'loadConfig', 'ngmin', 'uglify', 'cssmin', 'copy']);
    

    The copy task requires to install grunt-copy module

  3. Now it's time to make single application.js, application.min.js & application.min.css files for production (see the /public/dist folder). Run in the app folder

    $grunt build
    
  4. Copy files to production server. I prefer to use GIT push deployment. It sends to server only incremental changes. If you use GIT for the push-deployment it needs to add all files from `/public/dist/' folder into the repository.

  5. Because you use express.js in your project it's enough to use command

    $NODE_ENV=production node server.js&
    

    I know some developers use forever (module for node.js), but I prefer to use UPSTART (event-based init daemon) which available by default on Ubuntu as system service. I create a config file in /etc/init folder, e.g. /etc/init/my-app.conf. After this I can start, stop, restart my app as a service. E.g. service my-app start|stop|restart

    The UPSTART will monitor and restart your service in case of failure (see respawn command in the UPSTART config). You can find detailed answer how to make UPSTART config file here: upstart script for node.js

  6. (Optional, but recommended) Install nginx in front of Node.js. It's recommended to run you web app under unprivileged user due to security reasons, on the other hand if you want to use port 80 (it's a default port for the http protocol) for your web app (e.g. http://my-app-domain.com/ instead of http://my-app-domain.com:3000/) such configuration may be tricky. Because of this I use Nginx (port 80) in front of my web app which actually works on the port available for unprivileged user (e.g. 3000)

    6a. Instead of Nginx you can use Varnish