{"id":713,"date":"2020-11-17T22:20:37","date_gmt":"2020-11-17T21:20:37","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=713"},"modified":"2020-12-31T03:33:35","modified_gmt":"2020-12-31T02:33:35","slug":"how-to-package-angular-app-with-nodejs-backend-for-production","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2020\/11\/17\/how-to-package-angular-app-with-nodejs-backend-for-production\/","title":{"rendered":"How To Package Angular App With NodeJS Backend For Production"},"content":{"rendered":"\n<p>There are so many ways we can package and ship the Angular app to production. There are serverless and traditional architectures. In traditional architecture, we have different ways: Nodejs, Java, Python, etc.<\/p>\n\n\n\n<p>In this post, we are going to discuss how we can package the Angular app with nodejs backend. Finally, we will deploy this packaged application on AWS Elastic Beanstalk.<\/p>\n\n\n\n<h4><strong><em>Example Project<\/em><\/strong><\/h4>\n\n\n\n<p>This is a simple project which demonstrates developing and running Angular application with NodeJS. We have a simple app in which we can add users, count, and display them at the side, and retrieve them whenever you want.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/miro.medium.com\/max\/1200\/0*e_CzqQ5XnJwO7yH4.gif\" alt=\"Image for post\" width=\"453\" height=\"263\"\/><\/figure>\n\n\n\n<p>Here is the related article:  <a href=\"https:\/\/nguenkam.com\/blog\/index.php\/2020\/11\/17\/how-to-develop-and-build-angular-app-with-nodejs\/\" data-type=\"post\" data-id=\"710\">How To Develop and Build Angular App With NodeJS<\/a> .<\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">1. <strong><em>Manual Implementation<\/em><\/strong><\/span><\/h4>\n\n\n\n<p>In this manual implementation, we build the Angular app and place the appropriate code into one folder and run or deploy the application. As a first step, we need to build the Angular app and all the static assets and built files are placed into the&nbsp;<strong>dist<\/strong>&nbsp;folder.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ change to my-app directory\ncd my-app\n\n\/\/ build the app\nnpm run build<\/code><\/pre>\n\n\n\n<p>All the built and static assets are placed under this folder&nbsp;<strong>dist\/angular-nodejs-example<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1010\/1*7sC7yGvrJBo56mfHV2MM8w.png\" alt=\"Image for post\"\/><figcaption><strong>built\/static assets<\/strong><\/figcaption><\/figure>\n\n\n\n<p>Once you built the application, all you need to do is create a separate folder and place the nodejs related stuff in that folder. Let\u2019s create a folder called&nbsp;<strong>angular-nodejs<\/strong>&nbsp;and put server.js, package.json, and dist folder inside it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1334\/1*nFSZuCNcJuCP6TWDoDyo9g.png\" alt=\"Image for post\"\/><figcaption><strong>angular-nodejs<\/strong><\/figcaption><\/figure>\n\n\n\n<h4 id=\"be60\">Run the application<\/h4>\n\n\n\n<p id=\"9f21\">Let&#8217;s run the app by importing the whole folder angular-nodejs into VSCode editor and install the dependencies for the server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ install dependencies\nnpm install\n\n\/\/ run the app\nnpm start (node server.js)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1490\/1*WqP85UuCSJCiYtAiyXUrnQ.png\" alt=\"Image for post\"\/><figcaption><strong>server listening on 8081<\/strong><\/figcaption><\/figure>\n\n\n\n<p>The app is running on the server port&nbsp;<strong>8081.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1989\/1*1Q4RMA1b00fd3-lFCMlINg.png\" alt=\"Image for post\"\/><figcaption><strong>nodejs serving the assets on 8081<\/strong><\/figcaption><\/figure>\n\n\n\n<h4 id=\"6657\">Disadvantages<\/h4>\n\n\n\n<p id=\"b3da\">All the below steps should be done manually and these are time-consuming tasks.<\/p>\n\n\n\n<ul><li>We have to build Angular code manually<\/li><li>We have to place all the built files into a separate folder<\/li><li>We need to install node dependencies before we run the app<\/li><\/ul>\n\n\n\n<h4 id=\"962c\"><strong><em><span class=\"has-inline-color has-vivid-red-color\">2. With Webpack<\/span><\/em><\/strong><\/h4>\n\n\n\n<p id=\"d37e\">In the above implementation, once we put everything in the folder we need to install dependencies for the nodejs server to run the app. This is an additional step we need to do before running the app.<\/p>\n\n\n\n<p id=\"a2c2\">We can skip this step with the webpack. When we build the Angular code, the Angular CLI uses a webpack internally to build and bundle the entire code into few files. We can use the same for the nodejs server as well.<\/p>\n\n\n\n<p id=\"362b\">First, we need to install a webpack globally and in the project as well.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ install webpack\nnpm install webpack -g\nnpm install webpack --save<\/code><\/pre>\n\n\n\n<p>We need to have a&nbsp;<strong>webpack.config.js&nbsp;<\/strong>in the root folder since a webpack looks for this file. Here is the file. We have an entry file and output file and it is placed in the root folder.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const path = require('path');\n\nmodule.exports = {\n  entry: '.\/server.js',\n  mode: 'production',\n  target: 'node',\n  output: {\n    path: path.resolve(__dirname, '.'),\n    filename: 'server.bundle.js'\n  }\n};<\/code><\/pre>\n\n\n\n<p>Let\u2019s build and bundle it. All you need to do is to run this command&nbsp;<code>webpack<\/code>&nbsp;and the webpack looks for this file called&nbsp;<strong>webpack.config.js&nbsp;<\/strong>andbuild the entire server code and put it into one file called<strong>&nbsp;server.bundle.js.&nbsp;<\/strong>Here is the modified package.json file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"name\": \"angular-nodejs-example\",\n  \"version\": \"1.0.0\",\n  \"description\": \"node server\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"start\": \"node server.bundle.js\",\n    \"build\": \"webpack\",\n    \"dev\": \"nodemon .\/server.js localhost 3080\",\n    \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https:\/\/github.com\/bbachi\/angular-nodejs-example.git\"\n  },\n  \"author\": \"Bhargav Bachina\",\n  \"license\": \"ISC\",\n  \"bugs\": {\n    \"url\": \"https:\/\/github.com\/bbachi\/angular-nodejs-example\/issues\"\n  },\n  \"homepage\": \"https:\/\/github.com\/bbachi\/angular-nodejs-example#readme\",\n  \"dependencies\": {\n    \"express\": \"^4.17.1\",\n    \"webpack\": \"^4.42.1\"\n  },\n  \"devDependencies\": {\n    \"nodemon\": \"^2.0.2\"\n  }\n}<\/code><\/pre>\n\n\n\n<p>If the filename is different than webpack.config.js you need to pass that filename with the webpack command&nbsp;<code>webpack &lt;filename&gt;.<\/code>&nbsp;Once you build the server code all you need is server.bundle.js file. You don\u2019t even need any packages, package.json, etc.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1332\/1*txiWJsqoxW-kBfZK5-MTDg.png\" alt=\"Image for post\"\/><figcaption><strong>angular-nodejs<\/strong><\/figcaption><\/figure>\n\n\n\n<p>With this, we can skip the step&nbsp;<code>npm install<\/code>&nbsp;(installing dependencies) and you can just run&nbsp;<code>node server.bundle.js<\/code>&nbsp;and you can see the app running on port&nbsp;<strong>8081.<\/strong><\/p>\n\n\n\n<h4 id=\"acf8\">Disadvantages<\/h4>\n\n\n\n<p id=\"c473\">The only thing we have solved is to skipping node dependencies installation. There are still things we are doing here manually.<\/p>\n\n\n\n<ul><li>We have to build Angular code manually<\/li><li>We have to place all the built files into a separate folder<\/li><\/ul>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">3. <strong><em>Packaging With Gulp<\/em><\/strong><\/span><\/h4>\n\n\n\n<p id=\"3489\">In the above sections, we have seen manual steps and these steps have to be eliminated. We can achieve complete automation with the&nbsp;<a href=\"https:\/\/gulpjs.com\/\">gulp<\/a>. All the following steps can be made automated with the gulp.<\/p>\n\n\n\n<ul><li>Clean the directory if exists<\/li><li>Create a directory if not exists to put all the production build<\/li><li>Build Angular code with ng build<\/li><li>Place the Angular code into production directory<\/li><li>Build the server code with the webpack<\/li><li>Place the server code into production directory<\/li><li>Finally, zip all the code.<\/li><\/ul>\n\n\n\n<p id=\"1912\">Let\u2019s install all the required gulp packages to accomplish the above points.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ install gulp globally\nnpm install gulp -g\n\/\/ install as dev dependency\nnpm install gulp gulp-zip fancy-log del webpack-stream --save-dev\n\/\/ gulp              - core library\n\/\/ gulp-zip          - zipping the code\n\/\/ fancy-log         - logging\n\/\/ del               - deleting files\/folders\n\/\/ webpack-stream    - Build with webpack<\/code><\/pre>\n\n\n\n<p>when you run the command&nbsp;<code>gulp<\/code>&nbsp;it looks for the gulpfile.js file and executes all the tasks mentioned in that file. We can execute these tasks one by one or in parallel with the help of these modules&nbsp;<code>series, parallel.<\/code>&nbsp;Here is the file gulpfile.js.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const { src, dest, series, parallel } = require('gulp');\nconst del = require('del');\nconst fs   = require('fs');\nconst zip = require('gulp-zip');\nconst log = require('fancy-log');\nconst webpack_stream = require('webpack-stream');\nconst webpack_config = require('.\/webpack.config.js');\nvar exec = require('child_process').exec;\n\nconst paths = {\n  prod_build: 'prod-build',\n  server_file_name: 'server.bundle.js',\n  angular_src: 'my-app\/dist\/**\/*',\n  angular_dist: 'prod-build\/my-app\/dist',\n  zipped_file_name: 'angular-nodejs.zip'\n};\n\nfunction clean()  {\n  log('removing the old files in the directory')\n  return del('prod-build\/**', {force:true});\n}\n\nfunction createProdBuildFolder() {\n\n  const dir = paths.prod_build;\n  log(`Creating the folder if not exist  ${dir}`)\n  if(!fs.existsSync(dir)) {\n    fs.mkdirSync(dir);\n    log('?  folder created:', dir);\n  }\n\n  return Promise.resolve('the value is ignored');\n}\n\nfunction buildAngularCodeTask(cb) {\n  log('building Angular code into the directory')\n  return exec('cd my-app &amp;&amp; npm run build', function (err, stdout, stderr) {\n    log(stdout);\n    log(stderr);\n    cb(err);\n  })\n}\n\nfunction copyAngularCodeTask() {\n  log('copying Angular code into the directory')\n  return src(`${paths.angular_src}`)\n        .pipe(dest(`${paths.angular_dist}`));\n}\n\nfunction copyNodeJSCodeTask() {\n  log('building and copying server code into the directory')\n  return webpack_stream(webpack_config)\n        .pipe(dest(`${paths.prod_build}`))\n}\n\nfunction zippingTask() {\n  log('zipping the code ')\n  return src(`${paths.prod_build}\/**`)\n      .pipe(zip(`${paths.zipped_file_name}`))\n      .pipe(dest(`${paths.prod_build}`))\n}\n\nexports.default = series(\n  clean,\n  createProdBuildFolder,\n  buildAngularCodeTask,\n  parallel(copyAngularCodeTask, copyNodeJSCodeTask),\n  zippingTask\n);<\/code><\/pre>\n\n\n\n<p>You can actually see some tasks are run one by one and others are in parallel. For example, copying Angular code and building server code (line 67) can be run in parallel because there is no dependency between these. With the gulpfile.js in place, all you need to do is issue this command&nbsp;<code>gulp<\/code>&nbsp;.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/miro.medium.com\/max\/1200\/1*x4DqxLttO1D028SJ7OSUGw.gif\" alt=\"Image for post\"\/><figcaption><strong>building with gulp<\/strong><\/figcaption><\/figure>\n\n\n\n<h4><strong><span class=\"has-inline-color has-vivid-red-color\">4. <em>With Docker<\/em><\/span><\/strong><\/h4>\n\n\n\n<p id=\"f18e\">We have seen different implementations so far. All these implementations include putting all the related files together and package it. We used different tools and bundlers to do that. But with Docker, we place all the files in the Docker file system and create a Docker image out of it.<\/p>\n\n\n\n<p id=\"f6a9\">Here is the complete post of how we can package with Docker: <a href=\"https:\/\/nguenkam.com\/blog\/index.php\/2020\/12\/31\/how-to-dockerize-angular-app-with-nodejs-backend\/\" data-type=\"post\" data-id=\"751\">How to Dockerize Angular App With NodeJS Backend<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-green-cyan-color\">Deploying on AWS Elastic Beanstalk<\/span><\/h4>\n\n\n\n<p>Packing your application is done now it\u2019s time to deploy that on the servers. You can deploy this package on different platforms or server architectures but, covering all those is out of scope for this post. Let us see one example of deployment of this application. Just follow the below post if you want to learn how to deploy this packaged app on AWS Elastic Beanstalk:<\/p>\n\n\n\n<p><a href=\"https:\/\/nguenkam.com\/blog\/index.php\/2020\/12\/31\/aws-how-to-deploy-angular-with-nodejs-app-on-elastic-beanstalk\/\" data-type=\"post\" data-id=\"753\">AWS \u2014 How to Deploy Angular With NodeJS App On Elastic Beanstalk<\/a><\/p>\n\n\n\n<h4 id=\"5d0f\"><strong><em>Summary<\/em><\/strong><\/h4>\n\n\n\n<ul><li>In traditional architectures, there are so many ways we can package and ship the Angular app to production.<\/li><li>If you are new to the Angular app with nodejs backend,\u00a0<a href=\"https:\/\/nguenkam.com\/blog\/index.php\/2020\/11\/17\/how-to-develop-and-build-angular-app-with-nodejs\/\" data-type=\"post\" data-id=\"710\">please follow this link to get familiar with it.<\/a><\/li><li>With the manual implementation, we have to build the Angular code, place the appropriate file and zipping the code manually.<\/li><li>We can automate all these tasks with the help of\u00a0<a href=\"https:\/\/gulpjs.com\/\">gulp<\/a>.<\/li><li>Angular CLI uses a webpack internally to build the Angular code. We can use the same with the NodeJS code as well. In this way, we can skip installing all the dependencies.<\/li><li>Docker is another way to package your application but you need to run those Docker images on some container platforms such as Docker, EKS, ECS, etc.<\/li><li>Always use multi-stage builds while building your Docker images so that you can avoid unnecessary files packaged into your build.<\/li><li>Always automate the tasks with some kind of tools such as gulp or grunt.<\/li><li>Elastic Beanstalk from AWS is an easier and quicker way to deploy your packaged app and test it out.<\/li><\/ul>\n\n\n\n<h4 id=\"eb3b\">Conclusion<\/h4>\n\n\n\n<p id=\"8f60\">Always automate packaging your app. In that way, you can save lots of time and be more productive.<\/p>\n\n\n\n<h5>reference:<\/h5>\n\n\n\n<p><a href=\"https:\/\/medium.com\/\">https:\/\/medium.com\/<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are so many ways we can package and ship the Angular app to production. There are serverless and traditional architectures. In traditional architecture, we have different ways: Nodejs, Java, Python, etc. In this post, we are going to discuss how we can package the Angular app with nodejs backend. Finally, we will deploy this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":716,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,145],"tags":[40,149,146,148,150],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/713"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=713"}],"version-history":[{"count":5,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/713\/revisions"}],"predecessor-version":[{"id":758,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/713\/revisions\/758"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/716"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}