{"id":2487,"date":"2016-01-12T06:24:23","date_gmt":"2016-01-12T06:24:23","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=2487"},"modified":"2016-01-12T06:28:34","modified_gmt":"2016-01-12T06:28:34","slug":"how-to-run-node-js-server-in-ionic-mobile-app","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/nodejs\/how-to-run-node-js-server-in-ionic-mobile-app\/","title":{"rendered":"How to run Node.js server in Ionic mobile app?"},"content":{"rendered":"<p><a href=\"http:\/\/stackoverflow.com\/users\/534755\/nikola\"><img loading=\"lazy\" decoding=\"async\" title=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" src=\"http:\/\/stackoverflow.com\/users\/flair\/534755.png\" rel=\"lightbox[2487]\" alt=\"profile for Nikola at Stack Overflow, Q&amp;A for professional and enthusiast programmers\" width=\"208\" height=\"58\" \/><\/a><br \/>\nI&#8217;m a big fan of <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> and I tend to contribute regularly (am currently in the <a href=\"http:\/\/stackexchange.com\/leagues\/1\/alltime\/stackoverflow\/2008-07-31\/534755?sort=reputationchange#534755\">top 0.X%<\/a>).\u00a0In this category (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/category\/stack-overflow\/\">stackoverflow<\/a>)\u00a0of posts, I will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread <a href=\"http:\/\/meta.stackoverflow.com\/questions\/266971\/can-i-post-so-questions-and-answers-in-a-personal-blog\/266973\">here<\/a>.<\/p>\n<p>As you may know, I&#8217;m really into Ionic framework lately\u00a0and am helping out on StackOverflow with the knowledge I gained so far with the framework. I&#8217;m currently <strong>#1<\/strong> in the <a href=\"http:\/\/stackoverflow.com\/tags\/ionic\/topusers\">top All time answerers<\/a> list.<\/p>\n<p>I answered <a href=\"http:\/\/stackoverflow.com\/questions\/34579550\/how-to-run-node-js-server-in-ionic-mobile-app\/34580053\">this question<\/a>\u00a0by user <a href=\"http:\/\/stackoverflow.com\/users\/3548251\/shubham\">Shubham<\/a>:<\/p>\n<p>I am making an app using MEAN and ionic framework where nodejs is a middleware to connect to the database(mongoDb). I need to run the nodejs server using <code>node server.js<\/code> and the app using <code>ionic serve<\/code>. This is my server.js.<\/p>\n<pre class=\"lang:default decode:true\">var express          = require('express'),\r\napp              = express(),\r\nbodyParser       = require('body-parser'),\r\nmongoose         = require('mongoose'),\r\nCohortController =require('.\/www\/server\/controller\/CohortController');\r\n\r\nmongoose.connect('mongodb:\/\/localhost:27017\/persistent');\r\n\r\napp.use(bodyParser());\r\n\r\napp.get('\/api\/cohorts',CohortController.list);\r\napp.post('\/api\/cohorts',CohortController.create);\r\n\r\napp.listen(3000,function(){\r\nconsole.log('Listening...');\r\n})<\/pre>\n<p>Now this is my app.js. I use <code>http:\/\/localhost:3000<\/code> to get the JSON.<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"lang:default decode:true\">app.controller('CohortController',['$scope','$resource',\r\n  function($scope,$resource){\r\n    var Cohort=$resource('http:\/\/localhost:3000\/api\/cohorts');\r\n    Cohort.query(function(results){\r\n      $scope.cohorts=results;\r\n    });\r\n    $scope.cohorts=[];\r\n\r\n    $scope.createCohort= function () {\r\n      var cohort=new Cohort();\r\n      cohort.name=$scope.CohortName;\r\n      cohort.id=$scope.CohortId;\r\n      cohort.$save(function(result){\r\n        $scope.cohorts.push(result);\r\n        $scope.CohortName='';\r\n        $scope.CohortId='';\r\n      });\r\n    }\r\n  }]);<\/pre>\n<\/div>\n<\/div>\n<p>How can I run the node server when I convert it into a mobile application? How the application will use the API?<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/34579550\/how-to-run-node-js-server-in-ionic-mobile-app\/34580053#34580053\">My answer<\/a> was:<\/p>\n<blockquote><p>You will have to have your Node.js app running on a server which you would then access (from your Ionic app) via it&#8217;s public IP. So, you wouldn&#8217;t use <code>http:\/\/localhost:3000<\/code> to get the JSON, instead you would use something like <code>http:\/\/123.456.789.123:3000<\/code>.<\/p>\n<p>But, usually, this is not the way you would do it (with the port 3000). What you would additionally do is put (for example) Nginx in front of your Node.js app (<a href=\"http:\/\/www.nikola-breznjak.com\/blog\/nodejs\/using-nginx-as-a-reverse-proxy-in-front-of-your-node-js-application\/\" rel=\"nofollow\">see an example here<\/a>) in order to serve your api from the standard HTTP port (80).<\/p>\n<p>So, basically, <strong>you can&#8217;t actually &#8220;run Node.js server in Ionic app&#8221;<\/strong> &#8211; the way you do it is run the Node.js app separate from Ionic and expose its functionality via a standardized API (usually these days <a href=\"https:\/\/en.wikipedia.org\/wiki\/Representational_state_transfer\" rel=\"nofollow\">REST<\/a>is what you would want to achieve) which you then &#8220;consume&#8221; via Ionic&#8217;s (well, to be exact, it&#8217;s actually Angular&#8217;s) <a href=\"https:\/\/docs.angularjs.org\/api\/ngResource\/service\/$resource\" rel=\"nofollow\">$resource module<\/a>.<\/p>\n<p>Hope this helps clear things up a bit.<\/p><\/blockquote>\n<blockquote class=\"twitter-tweet\" data-width=\"550\">\n<p lang=\"en\" dir=\"ltr\">How to run <a href=\"https:\/\/twitter.com\/hashtag\/Node?src=hash\">#Node<\/a>.js server in <a href=\"https:\/\/twitter.com\/hashtag\/Ionic?src=hash\">#Ionic<\/a> mobile app? <a href=\"https:\/\/t.co\/8SOUYGM3Vf\">https:\/\/t.co\/8SOUYGM3Vf<\/a><\/p>\n<p>&mdash; Nikola Bre\u017enjak (@HitmanHR) <a href=\"https:\/\/twitter.com\/HitmanHR\/status\/686796811122651136\">January 12, 2016<\/a><\/p><\/blockquote>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%).\u00a0In this category (stackoverflow)\u00a0of posts, I will be posting my&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1991,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,4,35],"tags":[],"class_list":["post-2487","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ionic","category-nodejs","category-stack-overflow"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=2487"}],"version-history":[{"count":3,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2487\/revisions"}],"predecessor-version":[{"id":2490,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/2487\/revisions\/2490"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/1991"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=2487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=2487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=2487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}