5

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 node server.js and the app using ionic serve. This is my server.js.

var express          = require('express'),
app              = express(),
bodyParser       = require('body-parser'),
mongoose         = require('mongoose'),
CohortController =require('./www/server/controller/CohortController');

mongoose.connect('mongodb://localhost:27017/persistent');

app.use(bodyParser());

app.get('/api/cohorts',CohortController.list);
app.post('/api/cohorts',CohortController.create);

app.listen(3000,function(){
console.log('Listening...');
})

Now this is my app.js. I use http://localhost:3000 to get the JSON.

app.controller('CohortController',['$scope','$resource',
  function($scope,$resource){
    var Cohort=$resource('http://localhost:3000/api/cohorts');
    Cohort.query(function(results){
      $scope.cohorts=results;
    });
    $scope.cohorts=[];

    $scope.createCohort= function () {
      var cohort=new Cohort();
      cohort.name=$scope.CohortName;
      cohort.id=$scope.CohortId;
      cohort.$save(function(result){
        $scope.cohorts.push(result);
        $scope.CohortName='';
        $scope.CohortId='';
      });
    }
  }]);

How can I run the node server when I convert it into a mobile application? How the application will use the API?

2
  • Will you run mongodb on your mobile server too ?
    – c-smile
    Jan 3, 2016 at 17:57
  • I will use mongolab to host the mongdb. Jan 3, 2016 at 18:47

1 Answer 1

5

You will have to have your Node.js app running on a server which you would then access (from your Ionic app) via it's public IP. So, you wouldn't use http://localhost:3000 to get the JSON, instead you would use something like http://123.456.789.123:3000.

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 (see an example here) in order to serve your api from the standard HTTP port (80).

So, basically, you can't actually "run Node.js server in Ionic app" - 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 REST is what you would want to achieve) which you then "consume" via Ionic's (well, to be exact, it's actually Angular's) $resource module.

Hope this helps clear things up a bit.

9
  • I might need to ask this as another question. Lets say I want the node API/Server to allow calls from the ionic app running on someones phone and a web site (example.com). It sounds like I need to enable CORS, but I don’t want to open it up to attacks so from reading, it sounds like we don’t want to do: ‘Access-Control-Allow-Origin’, ‘*’. If I just wanted the website I would do ‘Access-Control-Allow-Origin’, ‘example.com’. From your answer I am still unsure how to allow calls from phones running the ionic app.
    – Beastwood
    Mar 23, 2016 at 15:19
  • @Beastwood: you would "simply" create REST api for your Node app, and that's it. I've put simply in quotes since it's not that "easy", but then again, it's not rocket science also.
    – Nikola
    Mar 23, 2016 at 15:36
  • thanks, but I am still confused how you specify the access-control-allow-origin for the ionic app.
    – Beastwood
    Mar 23, 2016 at 16:32
  • But, erm, that's the thing - you don't. Why would you? If you want to make the API available only to "some" users, introduce a keywhich you would send with every request. You may have confused these things with Allowing access origin when testing locally.
    – Nikola
    Mar 23, 2016 at 16:34
  • 1
    @Tashi in that case you would probably wanna use something like a jwt. Or, some kind of a short-lived token (which you'd get after a successful login) which you'd "refresh".
    – Nikola
    Nov 8, 2021 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.