Using Node.JS as a backend

Hello everyone,

I’ve been looking for a framework to build a mobile app. I found Meteor but decided to go with Node.JS as it seems to scale more properly.

The app basically consists of an Instagram-like system where you can login and share photos to everyone.

Now, as I am learning about web technologies and feeling a bit lost, I was thinking it would be good to have a REST API with Node.JS to which the Ionic app front-end connects. Is that something you would recommend? Or are there any better back-end solutions compatible with Ionic?

Could someone please clear this up for me? I’m really not sure where to start, and I don’t even know if this Node + Ionic stack is a good choice for me. Also, I would love to see some example projects that have a REST authentication API, this would really help me.

Thanks in advance for your time and your help,
Ulysse.

3 Likes

I really need an answer. Or is there any other forum I can ask this on? I mean, isn’t the official Ionic forum the best place to ask?

You might also try https://ionic-worldwide.slack.com.

Also, here’s a tutorial (focused on Twilio integration with Ionic) that uses Node/Mongo with Ionic: https://www.airpair.com/ionic-framework/posts/push-notifications-using-ionic-framework Not sure how far it will get you, but might be worth looking over.

1 Like

Thanks for the links!

Hi there,

Personally I found that a NodeJS server running ExpressJS & MongoDB works out the best for me. I get full control over everything, and I could manually connect AWS/Google Cloud instances to it as well.

The RESTful APIs I create are usually hosted for free on Heroku then transferred to a paid hosting once it’s time to hit production.

I found this tutorial helpful when I got started:
Video: https://www.youtube.com/watch?v=uaizKlOXyfY
Text: https://devdactic.com/restful-api-user-authentication-1

2 Likes

Thanks a lot for the resources! Having your own server indeed seems like the way to go.

I found this project while looking on Github ( https://github.com/arthurkao/angular-drywall ).
It seems that it already has a RESTful API with authentication included, which the client uses.

It also looks like it uses Angular, so does it mean I can “convert” the web client to an Ionic project, keeping all the authentication system (API calls)?

Thank you for your answer.

1 Like

Here is what my usual Angular 1 API service looks like:

.service('API', function ($http, $q, APP_GLOBALS) {
  this.get = function (path) {
    if (APP_GLOBALS.DEBUG_MODE)
      console.log('GET request is being made', APP_GLOBALS.API_URL + path);
    return $q(function (resolve, reject) {
      return $http.get(APP_GLOBALS.API_URL + path).then(function (res) {
        resolve(res.data);
      }, function (err) {
        reject(err);
      });
    });
  };
  this.delete = function (path) {
    return $q(function (resolve, reject) {
      return $http.delete(APP_GLOBALS.API_URL + path).then(function (res) {
        resolve(res.data);
      }, function (err) {
        reject(err);
      });
    });
  };
  this.post = function (path, data) {
    return $q(function (resolve, reject) {
      return $http.post(APP_GLOBALS.API_URL + path, data).then(function (res) {
        resolve(res.data);
      }, function (err) {
        reject(err);
      });
    });
  };
  this.put = function (path, data) {
    return $q(function (resolve, reject) {
      return $http.put(APP_GLOBALS.API_URL + path, data).then(function (res) {
        resolve(res.data);
      }, function (err) {
        reject(err);
      });
    });
  };
})

Here is what APP_GLOBALS looks like:

.constant('APP_GLOBALS', {
  API_URL: 'https://my-heroku-app-name.herokuapp.com/api/',
  GOOGLE_MAPS_API_KEY: 'JustASampleGoogleAPIKey',
  DEBUG_MODE: true
});

Notice that I do not provide anything other than the path (and body, if posting) when I’m making a call. That’s because the Authroization headers are set by my login function. When I login, I get my JSON Web Token from the API. Then I store that token as my default header for every call that is made using $http.
$http.defaults.headers.common.Authorization = JWT_TOKEN;

Whenever I need to make a call to the server I just run the following code from my controller:

API.get('profile').then(
    function(res){
        // Got a response
    },
    function(error){
        // Got an error
});
1 Like

Awesome! I can’t thank you enough for your answer, this really clears it up! :grinning:

1 Like

No problem please let me know if you need anything else cleared up. Always glad to help!

2 Likes